Thursday, June 26, 2014

Spring4Netty without Web.XML configuration on Java 8 (JBoss WildFly and Tomcat 8) - part.2::netty and JSON


  In the introduction (part.1). we've pictured out the basic idea of the project  abstraction(car exchange;). In this part we are going to configure Netty as the part of maven spring module (s4netty-worker). 

  Let's draw the agenda that allows us to start netty server under the spring ecosystem:
  1. initiate serverBootstrap object and configure the Netty server from properties file (
  2. configure the group, channel, handler and some channel options (ps: keep alive signal has been ignored in this project)
  3. Channel initialisation  
  4. Netty JSON service response
  5. Netty JSON request handling
  Let's begin and configure WorkerNettyServer which contains the instance of the serverBootstrap bean (add.1.). ServerBootstrap definition can be found in WorkerNettyConfig class.
@Bean(name = "serverBootstrap")
public ServerBootstrap bootstrap() {
All necessary  properties for WorkerNettyConfig class used by serverBoostrap (add.1.) bean initialisation are taken from the config the file ( 
public class WorkerNettyConfig {
  To make possible reading from the *.properties file   
private int tcpPort;
we need to have access to the current Spring Context. This goal we achieve by using property sources place holder :
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
Now WorkerNettyConfig knows all values for its initiation 
@Bean(name = "serverBootstrap")
public ServerBootstrap bootstrap() {
   ServerBootstrap b = new ServerBootstrap();
After all those configuration steps, we get access, inside the WorkerNettyServer class, to the bean "serverBootstrap" by using Qualifier annotation for candidate Bean serverBootstrap (add.2.). 
public class WorkerNettyServer {
    private ServerBootstrap serverBootstrap;
and we can have quickly assign all serverBootstrap properties (add.2.
@Bean(name = "serverBootstrap")
public ServerBootstrap bootstrap() {
    ServerBootstrap b = new ServerBootstrap();, workerGroup())
  So move forward and initiate the Channel and it's pipeline in the WorkerInitializer class (add.3.). Of course all according to our "master plan" to have JSON service listening on the specified port P and exchanging car list with base.
  In this case we need totell Netty how it should deal with such type of the request by utilising HttpServerCodec (add.4.). 
pipeline.addLast(new HttpServerCodec());
  Currently it looks like JSON service  is listening on the port P and everything is almost ready to go. Well not exactly, there is still a some work do. 
  One good question could be about some missing definition :) oh, yes, where is 
workerProviderHandler ? (add.5.)  and here we go.
public class WorkerProviderHandler extends ChannelInboundHandlerAdapter {
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        if (msg instanceof HttpRequest) {
            HttpRequest req = (HttpRequest) msg; 
  The workerProviderHandler is the extension of ChannelInboundHandlerAdapter and it handles our request and provides the JSON response to the requestor (s4netty-web). 
The request itself consists from results provided by spring service. 
SimpleCarService simpleCarService;
Gson library has been used to serialise and deserialise POJO objects.

After defining handler and assigning to the pipeline we are done with Spring-Netty Configuration and ready to define module (s4netty-worker) Spring MVC without Web.xml usage. 
Sure, in the next part (Click Next

s4netty project GiTHub repository

No comments: