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:<br>
<br>public class DasGatewayServer<br>{<br> private Channel serverChannel;<br> private ChannelFactory channelFactory;<br> private ChannelGroup channelGroup;<br> private ServerBootstrap bootstrap;<br> private ChannelPipelineFactory channelPipelineFactory;<br>
private Map<String, Object> options = new HashMap<String, Object>();<br> private int port;<br> <br> @Required<br> public void setChannelGroup(ChannelGroup group)<br> {<br> this.channelGroup = group;<br>
}<br> <br> @Required<br> public void setChannelPipelineFactory(ChannelPipelineFactory channelPipelineFactory)<br> {<br> this.channelPipelineFactory = channelPipelineFactory;<br> }<br><br> @Required<br>
public void setChannelFactory(ChannelFactory factory)<br> {<br> this.channelFactory = factory;<br> }<br> <br> public void setOptions(Map<String, Object> options)<br> {<br> this.options = options;<br>
}<br><br> @Required<br> public void setPort(int port)<br> {<br> this.port = port;<br> }<br> <br> public void startup()<br> {<br> bootstrap = new ServerBootstrap(channelFactory);<br> bootstrap.setPipelineFactory(channelPipelineFactory);<br>
bootstrap.setOptions(options);<br> serverChannel = bootstrap.bind(new InetSocketAddress(port));<br> channelGroup.add(serverChannel);<br> }<br> <br> public void shutdown()<br> {<br> ChannelGroupFuture future = channelGroup.close();<br>
future.awaitUninterruptibly();<br> channelFactory.releaseExternalResources();<br> }<br>}<br><br>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:<br>
<br> <bean id="nettyServer" class="com.....Server" init-method="startup" destroy-method="shutdown"><br> <property name="channelFactory" ref="channelFactory"/><br>
<property name="channelGroup" ref="channelGroup"/><br> <property name="port" value="${server.port}"/><br> <property name="channelPipelineFactory" ref="serverChannelPipelineFactory"/><br>
</bean><br><br><br><br><div class="gmail_quote">On Sun, Jan 9, 2011 at 9:25 AM, Marco Mistroni <span dir="ltr"><<a href="mailto:mmistroni@gmail.com">mmistroni@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hello all<br>i am starting to use Netty, and i have couple of questions:<br><br>1 - is anyone using Netty together with Spring framework?<br>It seems weird to me that to startup Netty i would just use a standalone class with a main() method where i configure everything<br>
<br>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<br>
<br><br>w/kindest regards<br><font color="#888888"> Marco<br>
</font><br>_______________________________________________<br>
netty-users mailing list<br>
<a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br></blockquote></div><br>