Hi,
another Grizzly 2.0 example shows how easy it's possible to track
connection life-cycle (open, close) with filters.
Filters in Grizzly 2.0 are able to handle accept, connect and close
events, occurred on a Connection. These methods are called, once a
connection has been accepted, connected, closed.
The example contains 2 classes.
1) LifeCycleExample is very similar to the EchoServer, but adds
additional LifeCycleFilter to the chain. And prints the connection
statistics.
public static void main(String[] args) throws IOException {
// Create TCP transport
TCPNIOTransport transport =
TransportManager.instance().createTCPTransport();
LifeCycleFilter lifeCycleFilter = new LifeCycleFilter();
// Add TransportFilter, which is responsible
// for reading and writing data to the connection
transport.getFilterChain().add(new TransportFilter());
// Add lifecycle filter to track the connections
transport.getFilterChain().add(lifeCycleFilter);
// Add echo filter
transport.getFilterChain().add(new EchoFilter());
try {
// binding transport to start listen on certain host and
port
transport.bind(HOST, PORT);
// start the transport
transport.start();
System.out.println("Press 'q and ENTER' to exit, or just
ENTER to see statistics...");
do {
printStats(lifeCycleFilter);
} while (System.in.read() != 'q');
} finally {
// stop the transport
transport.stop();
// release TransportManager resources like ThreadPool
TransportManager.instance().close();
}
}
/**
* Print the lifecycle statistics
*
* @param lifeCycleFilter the {_at_link LifeCycleFilter}
*/
private static void printStats(LifeCycleFilter lifeCycleFilter) {
System.out.println("The total number of connections ever
connected: " +
lifeCycleFilter.getTotalConnections());
System.out.println("The number of active connections: " +
lifeCycleFilter.getActiveConnections().size());
}
2) The LifeCycleFilter, where we handle 3 life-cycle related actions:
accept, connect, close.
For accept and connect - we add new connection to the map and for
close we remove the connection.
/**
* Method is called, when new {_at_link Connection} was
* accepted by a {_at_link Transport}
*
* @param ctx the filter chain context
* @param nextAction next action to be executed
* by the chain (could be modified)
* @return the next action to be executed by chain
* @throws java.io.IOException
*/
@Override
public NextAction handleAccept(FilterChainContext ctx, NextAction
nextAction) throws IOException {
newConnection(ctx.getConnection());
return nextAction;
}
/**
* Method is called, when new client {_at_link Connection} was
* connected to some endpoint
*
* @param ctx the filter chain context
* @param nextAction next action to be executed
* by the chain (could be modified)
* @return the next action to be executed by chain
* @throws java.io.IOException
*/
@Override
public NextAction handleConnect(FilterChainContext ctx,
NextAction nextAction) throws IOException {
newConnection(ctx.getConnection());
return nextAction;
}
/**
* Method is called, when the {_at_link Connection} is getting closed
*
* @param ctx the filter chain context
* @param nextAction next action to be executed
* by the chain (could be modified)
* @return the next action to be executed by chain
* @throws java.io.IOException
*/
@Override
public NextAction handleClose(FilterChainContext ctx, NextAction
nextAction) throws IOException {
activeConnectionsMap.remove(ctx.getConnection());
return super.handleClose(ctx, nextAction);
}
Complete sources are attached.
Thanks.
WBR,
Alexey.