Netty and Spring framework
Charles Hudak
meadandale at gmail.com
Sun Jan 9 12:43:59 EST 2011
I'm using Netty with Spring as well. Since so many of the handlers are
stateful (unlike MINA where everything was stateless) and there is no
channel pipeline factory implementation that you can use I found myself
having to write a server class that handles some of the wiring together. I'm
not fond of it but it works. I have it tied into the spring lifecycle so
that spring starts it up and shuts it down:
public class DasGatewayServer
{
private Channel serverChannel;
private ChannelFactory channelFactory;
private ChannelGroup channelGroup;
private ServerBootstrap bootstrap;
private ChannelPipelineFactory channelPipelineFactory;
private Map<String, Object> options = new HashMap<String, Object>();
private int port;
@Required
public void setChannelGroup(ChannelGroup group)
{
this.channelGroup = group;
}
@Required
public void setChannelPipelineFactory(ChannelPipelineFactory
channelPipelineFactory)
{
this.channelPipelineFactory = channelPipelineFactory;
}
@Required
public void setChannelFactory(ChannelFactory factory)
{
this.channelFactory = factory;
}
public void setOptions(Map<String, Object> options)
{
this.options = options;
}
@Required
public void setPort(int port)
{
this.port = port;
}
public void startup()
{
bootstrap = new ServerBootstrap(channelFactory);
bootstrap.setPipelineFactory(channelPipelineFactory);
bootstrap.setOptions(options);
serverChannel = bootstrap.bind(new InetSocketAddress(port));
channelGroup.add(serverChannel);
}
public void shutdown()
{
ChannelGroupFuture future = channelGroup.close();
future.awaitUninterruptibly();
channelFactory.releaseExternalResources();
}
}
I created a channel factory implementation that knows how to construct a
pipeline with the correct handlers and it is defined as another spring bean
as are all the other collaborators of the class above. Then I simply wire it
up in spring:
<bean id="nettyServer" class="com.....Server" init-method="startup"
destroy-method="shutdown">
<property name="channelFactory" ref="channelFactory"/>
<property name="channelGroup" ref="channelGroup"/>
<property name="port" value="${server.port}"/>
<property name="channelPipelineFactory"
ref="serverChannelPipelineFactory"/>
</bean>
On Sun, Jan 9, 2011 at 9:25 AM, Marco Mistroni <mmistroni at gmail.com> wrote:
> Hello all
> i am starting to use Netty, and i have couple of questions:
>
> 1 - is anyone using Netty together with Spring framework?
> It seems weird to me that to startup Netty i would just use a standalone
> class with a main() method where i configure everything
>
> 2 - what's the best way to shut down a server programmatically? in the
> example guide it is shown how to shut down netty from via a Client. I was
> wondering how can i organize the serverside code so that when i receive a
> message like 'ADMIN_SHUTDOWN' i can gracefully shutdown everything rather
> than doing a System.exit
>
>
> w/kindest regards
> Marco
>
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20110109/09b39d28/attachment.html
More information about the netty-users
mailing list