|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sun.grizzly.http.embed.GrizzlyWebServer
public class GrizzlyWebServer
This class creates a WebServer that listen for http request. By default,
creating an instance of this class can be used as it is to synchronously serve resources located
under webResourcesPath. By default, the StaticResourcesAdapter is
used when there is no GrizzlyAdapter specified. The StaticResourcesAdapter
allow servicing of static resources like html files, images files, etc.
The GrizzlyAdapter provides developpers with a simple and consistent mechanism for extending
the functionality of the Grizzly WebServer and for bridging existing
http based technology like JRuby-on-Rail, Servlet, Bayeux Protocol or any
http based protocol.
You can extend the GrizzlyWebServer by adding one or serveral
GrizzlyAdapter. If more that one are used, the GrizzlyAdapterChain
will be used to map the request to its associated GrizzlyAdapters, using a Mapper.
A GrizzlyAdapter gets invoked
as soon as the http request has been parsed and
decoded. The GrizzlyAdapter.service(com.sun.grizzly.tcp.http11.GrizzlyRequest, com.sun.grizzly.tcp.http11.GrizzlyResponse)
method is invoked with a
GrizzlyRequest and GrizzlyResponse that can be used to extend the
functionality of the Web Server. By default, all http requests are synchronously
executed.
Asynchronous request processing is automatically enabled
as soon as one or several AsyncFilter are added, using the
#addAsyncHandler method. AsyncFilter can be used when
asynchronous operation are required, like suspending the current http request,
delaying the invokation of GrizzlyAdapter etc. The state of the
request processing can be managed using AsyncExecutor, like resuming
the process so GrizzlyAdapters can be invoked.
The following picture describes how AsyncFilter and GrizzlyAdapter
are invoked.
----------------------------------------------------------------------------
- AsyncFilter.doFilter() ---> AsyncExecutor.execute() -------| -
- | -
- | -
- | -
- AsyncFilter.doFilter() <-- GrizzlyAdapter2.service() <------| -
----------------------------------------------------------------------------
Here is some examples:
Synchronous Web Server servicing static resources
GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
try{
ws.start();
} catch (IOException ex){
// Something when wrong.
}
Synchronous Web Server servicing customized resources
GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
try{
ws.addGrizzlyAdapter(new GrizzlyAdapter(){
public void service(GrizzlyRequest request, GrizzlyResponse response){
try {
response.getWriter().println("Grizzly is soon cool");
} catch (IOException ex) {
}
}
});
ws.start();
} catch (IOException ex){
// Something when wrong.
}
Synchronous Web Server servicing a Servlet
GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
try{
ServletAdapter sa = new ServletAdapter();
sa.setRootFolder("/Path/To/Exploded/War/File");
sa.setServlet(new MyServlet());
ws.addGrizzlyAdapter(sa);
ws.start();
} catch (IOException ex){
// Something when wrong.
}
Synchronous Web Server servicing two Servlet
GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
try{
ServletAdapter sa = new ServletAdapter();
sa.setRootFolder("/Path/To/Exploded/War/File");
sa.setServlet(new MyServlet());
ws.addGrizzlyAdapter(sa);
ServletAdapter sa = new ServletAdapter();
sa.setRootFolder("/Path/To/Exploded/War2/File");
sa.setServlet(new MySecondServlet());
ws.addGrizzlyAdapter(sa);
ws.start();
} catch (IOException ex){
// Something when wrong.
}
Asynchronous Web Server servicing customized resources
The example below delay the request processing for 10 seconds, without holding
a thread.
GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
try{
ws.addAsyncFilter(new AsyncFilter() {
private final ScheduledThreadPoolExecutor scheduler =
new ScheduledThreadPoolExecutor(1);
public boolean doFilter(final AsyncExecutor asyncExecutor) {
//Throttle the request
scheduler.schedule(new Callable() {
public Object call() throws Exception {
asyncExecutor.execute();
asyncExecutor.postExecute();
return null;
}
}, 10, TimeUnit.SECONDS);
// Call the next AsyncFilter
return true;
}
});
ws.addGrizzlyAdapter(new GrizzlyAdapter(){
public void service(GrizzlyRequest request, GrizzlyResponse response){
try {
response.getWriter().println("Grizzly is soon cool");
} catch (IOException ex) {
}
}
});
ws.start();
} catch (IOException ex){
// Something when wrong.
}
Asynchronous Web Server servicing Servlet
The example below delay the request processing for 10 seconds, without holding
a thread.
GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
try{
ws.addAsyncFilter(new AsyncFilter() {
private final ScheduledThreadPoolExecutor scheduler =
new ScheduledThreadPoolExecutor(1);
public boolean doFilter(final AsyncExecutor asyncExecutor) {
//Throttle the request
scheduler.schedule(new Callable() {
public Object call() throws Exception {
asyncExecutor.execute();
asyncExecutor.postExecute();
return null;
}
}, 10, TimeUnit.SECONDS);
// Call the next AsyncFilter
return true;
}
});
ServletAdapter sa = new ServletAdapter();
sa.setRootFolder("/Path/To/Exploded/War/File");
sa.setServlet(new MyServlet());
ws.addGrizzlyAdapter(sa);
ws.start();
} catch (IOException ex){
// Something when wrong.
}
Asynchronous Web Server servicing Servlet and supporting the Bayeux Protocol
GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
try{
// Add Comet Support
ws.addAsyncFilter(new CometAsyncFilter());
//Add Bayeux support
CometdAdapter cometdAdapter = new CometdAdapter();
ws.addGrizzlyAdapter(cometdAdapter);
ServletAdapter sa = new ServletAdapter();
sa.setRootFolder("/Path/To/Exploded/War/File");
sa.setServlet(new MyServlet());
ws.addGrizzlyAdapter(sa);
ws.start();
} catch (IOException ex){
// Something when wrong.
}
Synchronous Web Server servicing static resources eand exposed using JMX Mbeans
GrizzlyWebServer ws = new GrizzlyWebServer(path);
ws.enableJMX(new Management() {
public void registerComponent(Object bean, ObjectName oname, String type)
throws Exception{
Registry.getRegistry().registerComponent(bean,oname,type);
}
public void unregisterComponent(ObjectName oname) throws Exception{
Registry.getRegistry().
unregisterComponent(oname);
}
});
ws.start();
}
}
Synchronous Web Server servicing two Servlet
GrizzlyWebServer ws = new GrizzlyWebServer(path);
ServletAdapter sa = new ServletAdapter();
sa.setRootFolder(".");
sa.setServletInstance(new ServletTest("Adapter-1"));
ws.addGrizzlyAdapter(sa, new String[]{"/Adapter-1"});
ServletAdapter sa2 = new ServletAdapter();
sa2.setRootFolder("/tmp");
sa2.setServletInstance(new ServletTest("Adapter-2"));
ws.addGrizzlyAdapter(sa2, new String[]{"/Adapter-2"});
System.out.println("Grizzly WebServer listening on port 8080");
ws.start();
* Synchronous Web Server servicing two Servlet using SSL
GrizzlyWebServer ws = new GrizzlyWebServer(443,path,5,true);
SSlConfig sslConfig = new SSLConfig();
sslConfig.setTrustStoreFile("Path-to-trustore");
sslConfig.setKeyStoreFile("Path-to-Trustore");
ws.setSSLConfig(sslConfig);
ServletAdapter sa = new ServletAdapter();
sa.setRootFolder(".");
sa.setServletInstance(new ServletTest("Adapter-1"));
ws.addGrizzlyAdapter(sa, new String[]{"/Adapter-1"});
ServletAdapter sa2 = new ServletAdapter();
sa2.setRootFolder("/tmp");
sa2.setServletInstance(new ServletTest("Adapter-2"));
ws.addGrizzlyAdapter(sa2, new String[]{"/Adapter-2"});
System.out.println("Grizzly WebServer listening on port 8080");
ws.start();
| Constructor Summary | |
|---|---|
GrizzlyWebServer()
Create a default GrizzlyWebServer |
|
GrizzlyWebServer(int port)
Create a WebServer that listen on port |
|
GrizzlyWebServer(int port,
int maxThreads)
Deprecated. use setMaxThreads(int) to set maximum number of
threads in a thread pool |
|
GrizzlyWebServer(int port,
int maxThreads,
String webResourcesPath)
Deprecated. use setMaxThreads(int) to set maximum number of
threads in a thread pool |
|
GrizzlyWebServer(int port,
int maxThreads,
String webResourcesPath,
boolean secure)
Deprecated. use setMaxThreads(int) to set maximum number of
threads in a thread pool |
|
GrizzlyWebServer(int port,
String webResourcesPath)
Create a WebServer that listen on port |
|
GrizzlyWebServer(int port,
String webResourcesPath,
boolean secure)
Create a WebServer that listen for secure tls/https requests |
|
GrizzlyWebServer(String webResourcesPath)
Create a WebServer which server files from webResourcesPath |
|
| Method Summary | |
|---|---|
void |
addAsyncFilter(AsyncFilter asyncFilter)
Add an AsyncFilter. |
void |
addGrizzlyAdapter(GrizzlyAdapter grizzlyAdapter)
Deprecated. - Use addGrizzlyAdapter(com.sun.grizzly.tcp.http11.GrizzlyAdapter, java.lang.String[]) |
void |
addGrizzlyAdapter(GrizzlyAdapter grizzlyAdapter,
String[] mapping)
Add a GrizzlyAdapter with its associated mapping. |
void |
enableJMX(Management jmxManagement)
Enable JMX Management by configuring the Management |
SelectorThread |
getSelectorThread()
Return the underlying SelectorThread. |
Statistics |
getStatistics()
Return a Statistics instance that can be used to gather
statistics. |
static GrizzlyWebServer |
newConfiguredInstance(String path)
Return an already configured GrizzlyWebServer that can serves
static resources. |
boolean |
removeGrizzlyAdapter(GrizzlyAdapter grizzlyAdapter)
Remove a GrizzlyAdapter
return true of the operation was successful. |
void |
setCoreThreads(int coreThreads)
Set the initial number of threads in a thread pool. |
void |
setMaxThreads(int maxThreads)
Set the maximum number of threads in a thread pool. |
void |
setSSLConfig(SSLConfig sslConfig)
Set the SSLConfig instance used when https is required |
void |
start()
Start the GrizzlyWebServer and start listening for http requests. |
void |
stop()
Stop the GrizzlyWebServer. |
void |
useAsynchronousWrite(boolean asyncWrite)
Set to true if you want to use asynchronous write operations. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public GrizzlyWebServer()
public GrizzlyWebServer(int port)
port - The port openedpublic GrizzlyWebServer(String webResourcesPath)
webResourcesPath
webResourcesPath - the path to the web resource (ex: /var/www)
public GrizzlyWebServer(int port,
int maxThreads)
setMaxThreads(int) to set maximum number of
threads in a thread pool
port - The port opened
public GrizzlyWebServer(int port,
String webResourcesPath)
port - The port openedwebResourcesPath - the path to the web resource (ex: /var/www)
public GrizzlyWebServer(int port,
int maxThreads,
String webResourcesPath)
setMaxThreads(int) to set maximum number of
threads in a thread pool
port - The port openedmaxThreads - The maximum number of Thread createdwebResourcesPath - the path to the web resource (ex: /var/www)
public GrizzlyWebServer(int port,
String webResourcesPath,
boolean secure)
port - The port openedwebResourcesPath - the path to the web resource (ex: /var/www)secure - true if https needs to be used.
public GrizzlyWebServer(int port,
int maxThreads,
String webResourcesPath,
boolean secure)
setMaxThreads(int) to set maximum number of
threads in a thread pool
port - The port openedmaxThreads - The maximum number of Thread createdwebResourcesPath - the path to the web resource (ex: /var/www)secure - true if https needs to be used.| Method Detail |
|---|
public SelectorThread getSelectorThread()
SelectorThread. Only advanced users
should manipulate that class.
SelectorThreadpublic void addAsyncFilter(AsyncFilter asyncFilter)
AsyncFilter. Adding AsyncFilter automatically
enable Grizzly Asynchronous Request Processing mode. AsyncFilters
are always invoked before GrizzlyAdapter
asyncFilter - An AsyncFilterpublic void addGrizzlyAdapter(GrizzlyAdapter grizzlyAdapter)
addGrizzlyAdapter(com.sun.grizzly.tcp.http11.GrizzlyAdapter, java.lang.String[])
GrizzlyAdapter. GrizzlyAdapter will be invoked by
Grizzly in the order they are added, e.g the first added is always the
first invoked. GrizzlyAdapters
are always invoked after AsyncFilter
grizzlyAdapter - a GrizzlyAdapter
public void addGrizzlyAdapter(GrizzlyAdapter grizzlyAdapter,
String[] mapping)
GrizzlyAdapter with its associated mapping. A request will
be dispatched to a GrizzlyAdapter based on its mapping value.
grizzlyAdapter - a GrizzlyAdaptermapping - An array contains the context path mapping information.public boolean removeGrizzlyAdapter(GrizzlyAdapter grizzlyAdapter)
GrizzlyAdapter
return true of the operation was successful.
public void setSSLConfig(SSLConfig sslConfig)
SSLConfig instance used when https is required
public void useAsynchronousWrite(boolean asyncWrite)
asyncWrite - true to enabled asynchronous write I/O operations.
public void start()
throws IOException
IOExceptionpublic void enableJMX(Management jmxManagement)
Management
jmxManagement - An instance of the Management interfacepublic Statistics getStatistics()
Statistics instance that can be used to gather
statistics. By default, the Statistics object is not
gathering statistics. Invoking Statistics.startGatheringStatistics()
will do it.
public void setCoreThreads(int coreThreads)
coreThreads - the initial number of threads in a thread pool.public void setMaxThreads(int maxThreads)
maxThreads - the maximum number of threads in a thread pool.public void stop()
public static final GrizzlyWebServer newConfiguredInstance(String path)
GrizzlyWebServer that can serves
static resources.
path - the directory to serve static resource from.
GrizzlyWebServer that listen on port 8080
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||