directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niklas Therning <nik...@trillian.se>
Subject [mina] Spring integration
Date Tue, 01 Nov 2005 21:54:16 GMT
Hi,

I've played around a bit with getting Mina to work nicely with Spring.
Right now the Mina classes are quite Dependency Injection unfriendly.
But by implementing a few Spring FactoryBeans it is possible to make
Mina easily configurable using Spring, at least for my needs.

Basically what I want is to be able to define the filters used by my
SocketConnector and SocketAcceptor and, for the SocketAcceptor, also the
bindings. This is what it looks like in Spring XML:

<bean id="threadPoolFilter" class="org.apache.mina.filter.ThreadPoolFilter">
  <constructor-arg value="WorkerThread"/><!-- This is the prefix used by
threads in the pool -->
  <property name="maximumPoolSize" value="10"/>
</bean>

<!-- My SocketAcceptor -->
<bean id="socketAcceptor"
class="org.apache.mina.spring.SocketAcceptorFactoryBean">
    <property name="filters">
      <map>
        <entry key="blacklist">
          <bean class="org.apache.mina.filter.BlacklistFilter">
            <property name="blacklist">
              <list>
                <!-- Put all hosts to be blacklisted here. -->
                <value>192.168.0.1</value>
                <value>127.0.0.1</value>
              </list>
            </property>
          </bean>
        </entry>
        <entry key="threadPool" value-ref="threadPoolFilter"/>
      </map>
    </property>
    <property name="bindings">
      <map>
        <!-- Bind to port 1234 on 127.0.0.1 and use myHandler as the
handler-->
        <entry key="127.0.0.1:1234" value-ref="myHandler"/>
      </map>
    </property>
  </bean>

<!-- My SocketConnector -->
  <bean id="socketConnector"
class="org.apache.mina.spring.SocketConnectorFactoryBean">
    <property name="filters">
      <map>
        <entry key="threadPool" value-ref="threadPoolFilter"/>
      </map>
    </property>
    <property name="connectTimeout" value="30"/>
  </bean>

<bean id="myHandler" class="MyHandler"/>


The only thing I've had to change in the original Mina code to make this
work is the "blacklist" property which have been added to the
BlackListFilter class.

SocketAcceptorFactoryBean will take care of binding and unbinding as the
Spring context is created and destroyed. No need for a ServiceRegistry
as I see it. Both SocketAcceptorFactoryBean and
SocketConnectorFactoryBean will add all configured filters to the end of
the filter chain.

WDYT? I will happily contribute this code back to the project if you
think it could be useful. The same could also be done for
VmPipe/Datagram/Acceptor/Connector.

/Niklas


Mime
View raw message