NOTE: The latest MINA releases doesn’t have the package specific to Spring, like its earlier versions. The package is now named Integration Beans, to make the implementation work for all DI frameworks.

Lets see the Spring xml file. Please see that I have removed generic part from xml and have put only the specific things needed to pull up the implementation. This example has been derived from Chat example shipped with MINA release.

Now lets pull things together

Lets set the IO Handler

Lets create the Filter chain

Here, we create instance of our IoFilter. See that for the ProtocolCodec factory, we have used Constructor injection. Logging Filter creation is straight forward. Once we have defined the beans for the filters to be used, we now create the Filter Chain to be used for the implementation. We define a bean with id “FilterChainBuidler” and add the defined filters to it. We are almost ready, and we just need to create the Socket and call bind

Lets complete the last part of creating the Socket and completing the chain

Now we create our ioAcceptor, set IO handler and Filter Chain. Now we have to write a function to read this file using Spring and start our application. Here’s the code

1

2

3

publicvoidinitializeViaSpring()throwsException{

newClassPathXmlApplicationContext("trapReceiverContext.xml");

}

We just call this method from main, and this shall initialize our MINA application. Will try to write a post using some other DI framework like Google Guice.

16 thoughts on “Integrating Apache MINA with Spring”

Hi Ashish,
Thanks for the example. One question that has been vexing me for the past while that you may know the answer to. Do you know why the reuseAddress session config parameter no longer exposed when configuring an NioDatagramConnector/Acceptor ? Of course it is possible to set it programmatically by getting a hold of the DatagramSessionConfig from the acceptor/connector once created, however it makes it awkward from a spring config point of view.

Spring supports nested property configuration by using OGNL path expressions. For example, if you wanted to change the ‘tcpNoDelay’ property of the NioSocketAcceptor’s nested SessionConfig instance:

…

…

Naturally the default,no-arg constructor of whatever bean you’re instantiating must ensure that any beans referenced via a OGNL expression are non-null (the NioSocketAcceptor does indeed ensure this for its internal SessionConfig instance). No coding necessary 🙂

Using the nested property technique above, I think you can easily satisfy almost all of your Mina Spring-configuration needs.

I see this is an old post but nevertheless, would it be possible to provide a simple example how to integrate mina app with webbap using guice so it would run on tomcat? Webapp already uses guice and as author suggested he could write a howto – I would very much appreciate just the vital example of it.

I have no clue how this can be done. Probably you need to implement a IoFilter that does the job for you, by integrating with Spring security API’s. Or look at existing integrations and tweak them to use your IoFilter.