users@grizzly.java.net

Re: Question about http on grizzly

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Thu, 24 Apr 2008 09:58:57 -0400

Salut,

ash2k! wrote:
> Hi, all!
>
> I need to implement a simple web server that will handle simple GET
> requests. Based on request server should do some actions and then answer the
> client by giving him a file (some thing like file server). Files will be big
> (~300-500 MB max) and i want to use NIO to be able to scale to many clients.
>
> Questions: :)
> 1) can i use comet or some other grizzly based thing to implement this?

Yes. What you want is to use the Grizzly's http web server. You can
download the binary here[1] and try it by doing:

java -jar grizzly-http-webserver-1.7.3.1.jar -p 8080 -a /var/www

(or build it yourself by checking out the code:

% svn checkout https://www.dev.java.net/svn/grizzly/trunk

Now this WebServer is for static resources. For you application, you
will need to write an Adapter[2] if you really want to work with bytes,
or if you want to use a Servlet-like API, extends the GrizzlyAdapter[3].
  I recommend you extend GrizzlyAdapter as it has support for
Input/OutputStream, Writer, etc. NIO API is not available at that layer
  but if you really needs access to the SocketChannel, I can probably
add a new API for you to get access to it.

Then once you have your Adapter implemented, you just need to do (the
code is from [4]):

> 83 /**
> 84 * Create a single SelectorThread.
> 85 * @param args The command line arguments.
> 86 */
> 87 public SelectorThread createSelectorThread(String args[]) throws Exception {
> 107 String selectorThreadClassname = System.getProperty(SELECTOR_THREAD);
> 108 if (selectorThreadClassname != null) {
> 109 st = (SelectorThread) loadClass(selectorThreadClassname);
> 110 } else {
> 111 st = new SelectorThread() {
> 112 @Override
> 113 public void listen() throws InstantiationException, IOException {
> 114 super.listen();
> 115 System.out.println("Server started in "
> 116 + (System.currentTimeMillis() - t1) + " milliseconds.");
> 117 }
> 118 };
> 119 }
> 120 st.setAlgorithmClassName(StaticStreamAlgorithm.class.getName());
> 121 st.setPort(port);
> 122 SelectorThread.setWebAppRootPath(appliPath);
> 123
> 124 st.setAdapter(configureAdapter(st));
> 125 return st;
> 126 }

And example of Adapter can be found in [5], a GrizzlyAdapter in [6]

> 2) may be there is some other (may be non grizzly based) http
> server/framework i can use to implement this?

Grizzly is exactly what you want :-)

A+

-- Jeanfrancois

[1]
http://download.java.net/maven/2/com/sun/grizzly/grizzly-http-webserver/1.7.3.1/grizzly-http-webserver-1.7.3.1.jar
[2] https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/tcp/Adapter.html
[3]
https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/tcp/http11/GrizzlyAdapter.html
[4]
https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/standalone/StandaloneMainUtil.html
[5]
https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/tcp/StaticResourcesAdapter.html
[6]
https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/http/servlet/ServletAdapter.html

>
> p.s. sorry for my english

Better than mine :-)