Hello,
Need your help for my problem. I'm trying to write json <-> db service with
custom 'nio' connection to my database (no jdbc driver). In short the code
so far looks lie:
public static void main(String[] args) throws IOException {
TCPNIOTransport tcpTransport = TCPNIOTransportBuilder.newInstance().build();
tcpTransport.start();
final FilterChain clientFilterChain = FilterChainBuilder.stateless()
.add(new TransportFilter())
.build();
TCPNIOConnectorHandler connectorHandler =
TCPNIOConnectorHandler.builder(tcpTransport)
.processor(clientFilterChain)
.build();
SingleEndpointPool singleEndpointPool = SingleEndpointPool
//database connections
.builder(SocketAddress.class)
.connectorHandler(connectorHandler)
.endpointAddress(new
InetSocketAddress(InetAddress.getByName("localhost"), 12345))
.maxPoolSize(10)
.build();
HttpServer server = HttpServer.createSimpleServer(null, "localhost", 9090);
server.getServerConfiguration().addHttpHandler(new
MyHandler(singleEndpointPool), "/users");
server.start();
System.in.read();
}
then MyHandler (from
org.glassfish.grizzly.samples.httpserver.nonblockinghandler.UploadHttpHandlerSample):
...
public void service(Request request,
org.glassfish.grizzly.http.server.Response response) throws Exception
{
final NIOInputStream in = request.getNIOInputStream();
response.suspend();
in.notifyAvailable(new ReadHandler() {
................
@Override
public void onAllDataRead() throws Exception {
// getting connection to db
GrizzlyFuture<Connection> connectionFuture =
singleEndpointPool.take();
Connection connection = connectionFuture.get(10,
TimeUnit.MILLISECONDS);
//didn't find any better place to set timeouts, looks like
no way to do it through pool?
connection.setReadTimeout(1000, TimeUnit.MILLISECONDS);
connection.setWriteTimeout(1000, TimeUnit.MILLISECONDS);
//here code to make dbRq from http request
.......................
connection.write(new ByteBufferWrapper(dbRq), new
EmptyCompletionHandler<RecordWriteResult>() {
.....
@Override
public void completed(RecordWriteResult result) {
connection.read(new CompletionHandler<ReadResult>() {
HERE IS THE PROBLEM
Neither completed no failed are colled
});
}
});
}
});
}
The first question - is this code looks like optimal way to write
json/db service?
And then, if it is, why doesn't it receive anything from db? It
writes, I can see records inserted, and I'm sure db wants to return
something, at least "pure nio" implementation receives db responses.
Best regards, Eugene.