java - Netty HTTP server with Spring framework -


i implementing netty application spring; however, application shuts down after starting without exception.

my console output is:

connected target vm, address: '127.0.0.1:62313', transport: 'socket' log4j:warn no appenders found logger (org.springframework.core.env.standardenvironment). log4j:warn please initialize log4j system properly. log4j:warn see http://logging.apache.org/log4j/1.2/faq.html#noconfig more info. disconnected target vm, address: '127.0.0.1:62313', transport: 'socket' server started  process finished exit code 0 

my code is:

iserver:

package org.crytek.server;  /**  * created eemrcag on 19.06.2015.  */ public interface iserver {      public void start();      public void restart();      public void stop(); } 

servernettyimpl:

package org.crytek.server;  import io.netty.bootstrap.serverbootstrap; import io.netty.channel.channel; import io.netty.channel.channelfuturelistener; import io.netty.channel.channeloption; import io.netty.channel.nio.nioeventloopgroup; import io.netty.channel.socket.nio.nioserversocketchannel; import io.netty.handler.logging.loglevel; import io.netty.handler.logging.logginghandler; import org.crytek.server.handler.cryserverinitializer; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.stereotype.component;  import java.net.inetsocketaddress;  /**  * created eemrcag on 19.06.2015.  */ @component("servernettyimpl") public class servernettyimpl implements iserver{      @autowired     @qualifier("serverinitializer")     private cryserverinitializer serverinitializer;      @autowired     @qualifier("bossgroup")     private nioeventloopgroup bossgroup;      @autowired     @qualifier("workergroup")     private nioeventloopgroup workergroup;  @autowired @qualifier("tcpsocketaddress") private inetsocketaddress tcpsocketaddress;      private channel channel;          @override     public void start() {         serverbootstrap b = new serverbootstrap();         b.option(channeloption.so_backlog, 1024);         b.group(bossgroup,workergroup)                 .channel(nioserversocketchannel.class)                 .handler(new logginghandler(loglevel.info))                 .childhandler(serverinitializer);              try {                 channel = b.bind(tcpsocketaddress).sync().channel().closefuture().channel();                 system.out.println("server started");             } catch (interruptedexception e) {                 e.printstacktrace();             }          }      @override     public void restart() {     this.stop();         this.start();     }      @override     public void stop() {         if (this.channel != null) {             this.channel.close().addlistener(channelfuturelistener.close);         }     } } 

serverconfig:

package org.crytek.server.config;  import io.netty.bootstrap.serverbootstrap; import io.netty.channel.channeloption; import io.netty.channel.eventloopgroup; import io.netty.channel.nio.nioeventloopgroup; import io.netty.channel.socket.nio.nioserversocketchannel; import io.netty.handler.logging.loglevel; import io.netty.handler.logging.logginghandler; import org.crytek.server.handler.cryserverinitializer; import org.crytek.server.humanpyramid.humanpyramidcalculator; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.beans.factory.annotation.value; import org.springframework.context.annotation.*; import org.springframework.context.support.propertysourcesplaceholderconfigurer;  import java.net.inetsocketaddress;  /**  * created eemrcag on 17.06.2015.  */ @configuration @componentscan("org.crytek.server") @propertysource("classpath:cryserver.properties") public class serverconfig {      @value("${port}")     private int port;      @value("${boss.thread.count}")     private int bossthreadcount;      @autowired     @qualifier("serverinitializer")     private cryserverinitializer serverinitializer;       @bean(name = "bossgroup", destroymethod = "shutdowngracefully")     public nioeventloopgroup bossgroup() {         return new nioeventloopgroup(bossthreadcount);     }      @bean(name = "workergroup", destroymethod = "shutdowngracefully")     public nioeventloopgroup workergroup() {         return new nioeventloopgroup();     }     @bean     public static propertysourcesplaceholderconfigurer propertyplaceholderconfigurer() {         return new propertysourcesplaceholderconfigurer();     }      @bean(name = "tcpsocketaddress")     public inetsocketaddress tcpport() {         return new inetsocketaddress(port);     }      @bean(name = "humanpyramidcalculator")     @scope("singleton")     public humanpyramidcalculator humanpyramidcalculator()     {         return new humanpyramidcalculator();     }  } 

cryserverhandler:

package org.crytek.server.handler;  import io.netty.buffer.bytebuf; import io.netty.buffer.bytebufutil; import io.netty.buffer.unpooled; import io.netty.channel.channelfuturelistener; import io.netty.channel.channelhandler.sharable; import io.netty.channel.channelhandleradapter; import io.netty.channel.channelhandlercontext; import io.netty.handler.codec.http.*; import org.crytek.server.humanpyramid.humanpyramidcalculator; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.stereotype.component;  import java.util.list;  import static io.netty.handler.codec.http.httpheadernames.content_length; import static io.netty.handler.codec.http.httpheadernames.content_type;  /**  * created eemrcag on 15.06.2015.  */  @component("serverhandler") @sharable public class cryserverhandler extends channelhandleradapter {      @autowired     @qualifier("humanpyramidcalculator")     private humanpyramidcalculator humanpyramidcalculator;      @override     public void channelreadcomplete(channelhandlercontext ctx) throws exception {         ctx.flush();     }      @override     public void channelread(channelhandlercontext ctx, object msg) throws exception {         if(msg instanceof httprequest)         {             httprequest req = (httprequest) msg;             string level = null;             string index = null;              //if(req.method().equals(httpmethod.get))             querystringdecoder decoder = new querystringdecoder(req.uri());             if(!decoder.path().equals("/humanedgeweight"))             {                 fullhttpresponse response = new defaultfullhttpresponse(httpversion.http_1_1, httpresponsestatus.not_found);                 ctx.channel().write(response).addlistener(channelfuturelistener.close);              }             list<string> levellist  = decoder.parameters().get("level");             if(levellist.size() != 1)             {                 //error             }else{                 level = levellist.get(0);             }             list<string> indexlist  = decoder.parameters().get("index");             if(indexlist.size() > 1)             {                 //error             }else{                 index = indexlist.get(0);             }             double result = 0.0;             if(index == null)             {                 result = humanpyramidcalculator.gethumanweight(integer.parseint(level));             }else{                 result = humanpyramidcalculator.gethumanweight(integer.parseint(level), integer.parseint(index));             }             bytebuf buf = unpooled.buffer();             bytebufutil.writeascii(buf,string.valueof(result));             fullhttpresponse response = new defaultfullhttpresponse(httpversion.http_1_1, httpresponsestatus.ok, buf);             response.headers().set(content_type, "text/plain");             response.headers().setint(content_length, response.content().readablebytes());             ctx.channel().write(response).addlistener(channelfuturelistener.close);         }     }      @override     public void exceptioncaught(channelhandlercontext ctx, throwable cause) throws exception {         cause.printstacktrace();         ctx.close();     } } 

cryserverinitializer:

package org.crytek.server.handler;  import io.netty.channel.channelinitializer; import io.netty.channel.channelpipeline; import io.netty.channel.socket.socketchannel; import io.netty.handler.codec.http.httpservercodec; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.stereotype.component;  /**  * created eemrcag on 16.06.2015.  */ @component("serverinitializer") public class cryserverinitializer extends channelinitializer<socketchannel> {     @autowired     @qualifier("serverhandler")     private cryserverhandler serverhandler;      @override     protected void initchannel(socketchannel socketchannel) throws exception {         channelpipeline p = socketchannel.pipeline();         p.addlast(new httpservercodec());         p.addlast(serverhandler);     }      public cryserverhandler getserverhandler() {         return serverhandler;     }      public void setserverhandler(cryserverhandler serverhandler) {         this.serverhandler = serverhandler;     } } 

helloapp:

package org.crytek.server;  import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext;  public class helloapp {     public static void main(string[] args) {         applicationcontext context = new classpathxmlapplicationcontext("spring-config.xml");         iserver server=(iserver)context.getbean("servernettyimpl");         server.start();     } } 

**helloservice **:

package org.crytek.server;  import org.springframework.stereotype.component;  @component public class helloservice {     public string sayhello() {         return "hello world!";     } } 

humanpyramidcalculator:

package org.crytek.server.humanpyramid;  /**  * created eemrcag on 16.06.2015.  */ public class humanpyramidcalculator {      public static double gethumanweight(int level)     {         return 1;     }     public static double gethumanweight(int level, int index)     {         return 1;     } } 

cryserver.properties:

port = 8080 boss.thread.count = 1 

spring-config.xml:

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"        xmlns:context="http://www.springframework.org/schema/context"        xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">     <context:annotation-config />     <context:component-scan base-package="org.crytek.server"/> </beans> 

any idea?

thanks.

you need end main method or server start method call blocks until shutdown. in netty helloworld example call channel.closefuture().sync()


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -