users@grizzly.java.net

Re: Using SPDY With Grizzly

From: Steve Curtis <stevo.curtis_at_googlemail.com>
Date: Fri, 10 Jun 2016 11:59:41 +0100

I’ve inherited a client-server application that creates a significant
amount of asynchronous ajax calls from the front end. We were using Simple
Framework for our backend and I have now swapped in Grizzly with the
intention of making use of SPDY protocol to avoid the max 6 browser
connections we are seeing at the moment.
>
>
>
> I have the application standing up fine, but it still seems to use
standard HTTP 1.1 over SSL and not SPDY.
>
>
>
> The code I’ve written is:
>
>
>
> private URI uri;
>
> private HttpServer server;
>
> private NetworkListener listener;
>
>
>
> public void runServer(int port) throws IOException
>
> {
>
> logger.info("starting grizzly framework server on port {}", port);
>
>
>
> ResourceConfig resourceConfig = new
ResourceConfig(JerseyResource.class);
>
> uri = UriBuilder.fromUri("https://localhost/").port(port).build();
>
> server = GrizzlyHttpServerFactory.createHttpServer(uri,
resourceConfig, true, createSSLContextConfigurator());
>
>
>
> listener = server.getListeners().iterator().next();
>
>
>
> listener.setSecure(true);
>
> NIOTransport nioTransport = TCPNIOTransportBuilder.newInstance()
>
> .setReuseAddress(true)
>
> .setIOStrategy(WorkerThreadIOStrategy.getInstance())
>
>
.setSelectorThreadPoolConfig(ThreadPoolConfig.defaultConfig().setCorePoolSize(2).setMaxPoolSize(4))
>
>
.setWorkerThreadPoolConfig(ThreadPoolConfig.defaultConfig().setCorePoolSize(3).setMaxPoolSize(20))
>
> .build();
>
> listener.setTransport((TCPNIOTransport)nioTransport);
>
>
>
> SpdyAddOn spdyAddOn = new SpdyAddOn(SpdyMode.NPN);
>
> listener.registerAddOn(spdyAddOn);
>
>
>
> final ServerConfiguration serverConfiguration =
server.getServerConfiguration();
>
> serverConfiguration.addHttpHandler(new HttpHandler()
>
> {
>
> public void
service(Request request, Response response) throws Exception
>
> {
>
> long startTime = new
Date().getTime();
>
>
>
> // Put a short sleep
in here so can see if requests queue up from browser
>
>
Thread.currentThread().sleep(500);
>
>
>
> // Get SPDY stream if
it exists
>
> final SpdyStream
spdyStream = (SpdyStream)
request.getAttribute(SpdyStream.SPDY_STREAM_ATTRIBUTE);
>
> // if spdy stream is
null it is not a SPDY based request
>
> if (spdyStream != null)
>
> {
>
> logger.info("found
a SPDY stream");
>
> }
>
> else
>
> {
>
> logger.info("no
SPDY stream available");
>
> }
>
> final SimpleDateFormat
format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.UK);
>
> final String date =
format.format(new Date(System.currentTimeMillis()));
>
>
response.setContentType("text/plain");
>
>
response.setContentLength(date.length());
>
>
response.getWriter().write(date);
>
> logger.info("processed
request in {} ms", new Date().getTime() - startTime);
>
> }
>
> }
>
> );
>
>
>
> server.start();
>
>
>
> logger.info("bootstrap of grizzly framework server complete,
running on {}", uri);
>
> }
>
>
>
> protected SSLEngineConfigurator createSSLContextConfigurator() throws
MalformedURLException
>
> {
>
>
>
> SSLContextConfigurator sslContextConfigurator = new
SSLContextConfigurator();
>
> ClassLoader classLoader = getClass().getClassLoader();
>
>
    sslContextConfigurator.setKeyStoreFile(classLoader.getResource("keystore.jks").getFile().toString());
>
> sslContextConfigurator.setKeyStorePass("changeit");
>
>
>
> sslContextConfigurator.validateConfiguration(true);
>
>
>
> SSLEngineConfigurator result = new SSLEngineConfigurator(
>
> sslContextConfigurator.createSSLContext(),
>
> false,
>
> false,
>
> false);
>
> result.setClientMode(false);
>
> return result;
>
> }
>
>
>
>
>
> The code is hosted on GitHub at
https://github.com/stevocurtis/public-development/blob/master/subprojects/playpens/java-playpen/web/grizzly-framework-playpen/src/main/java/com/fenixinfotech/grizzly/framework/playpen/JerseyGrizzlyFrameworkServer.java
>
>
>
> Notes:
>
>
>
> 1. I’ve run this against both open jdk 7 and oracle jdk 7.
>
> 2. I’ve tried bootstrapping with both the
grizzly-npn-bootstrap-1.0.jar and grizzly-npn-bootstrap-1.2.jar files.
>
> 3. You can see the dependencies in the pom but they are:
>
>
>
> jersey-container-grizzly2-http 2.35
>
> grizzly-http2 2.3.25
>
> grizzly-http-server 2.3.25
>
> grizzly-spdy 2.3.25
>
> grizzly-npn-bootstrap 1.2
>
>
>
> I’m wondering is there anything else I need to do to “trigger” initiation
of a SPDY protocol? Setting headers maybe.
>
>
>
> Thanks in advance,
>
>
>
> Steve