You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Michael Bauroth <Mi...@falcom.de> on 2006/08/01 01:21:42 UTC

3 questions regarding Mina and Spring

Hi,

I'm doing currently my first steps with the Springbased approach in 
Mina. Now I have a few questions how to convert my old code to the new 
style:

1. I'm configuring an alternate SocketAcceptor over the config file, 
which will be used for the later binding.

<property name="serviceConfig">
<bean class="org.apache.mina.transport.socket.nio.SocketAcceptorConfig">
<property name="filterChainBuilder" ref="filterChainBuilder"/>
<property name="reuseAddress" value="false"/>
<property name="disconnectOnUnbind" value="false"/>
</bean>
</property>

When I check the settings after server start, I get other values than 
specified. Here is the related code:

System.out.println("Starting server ...");
System.out.println("UseDirectBuffers " + ByteBuffer.isUseDirectBuffers());
System.out.println("DisconnectOnUnbind " + 
((SocketAcceptorConfig)mAcceptor.getDefaultConfig()).isDisconnectOnUnbind());
System.out.println("ReuseAddress " + 
((SocketAcceptorConfig)mAcceptor.getDefaultConfig()).isReuseAddress());

What I'm doing wrong?


2. In the original code I have specified some extra settings:

SocketSessionConfig tSessionConfig = 
(SocketSessionConfig)mAcceptorConfig.getSessionConfig();

tSessionConfig.setReuseAddress( REUSE_ADDRESS );
tSessionConfig.setTcpNoDelay( TCP_NO_DELAY );
tSessionConfig.setSendBufferSize( TCP_SND_BUFFER_SIZE );
tSessionConfig.setReceiveBufferSize( TCP_RCV_BUFFER_SIZE );
tSessionConfig.setSoLinger( SO_LINGER );
tSessionConfig.setOobInline( OOB_INLINE );
tSessionConfig.setKeepAlive( KEEP_ALIVE );

How can I specify these settings over the configuration xml file?


3. How can I request the SocketAddress from an acceptor after binding? I 
can't find a related method?


I hope, somebody can help me with some short advices ...


Thanx in advance!
Michael


Re: 3 questions regarding Mina and Spring

Posted by Niklas Therning <ni...@trillian.se>.
Michael Bauroth wrote:
> Hi Niklas,
>
> thank you very much for your help!!! Here is my modified and optimized
> config part:
>
> <bean id="acceptor"
> class="org.apache.mina.transport.socket.nio.SocketAcceptor"/>
> <bean id="defaultAcceptorConfig" factory-bean="acceptor"
> factory-method="getDefaultConfig">
>   <property name="filterChainBuilder" ref="filterChainBuilder"/>
>   <property name="reuseAddress" value="false"/>
>   <property name="disconnectOnUnbind" value="true"/>
> </bean>
>     
> <bean id="sessionConfig" factory-bean="defaultAcceptorConfig"
> factory-method="getSessionConfig">
>   <property name="reuseAddress" value="false"/>
>   <property name="tcpNoDelay" value="true"/>
>   <property name="sendBufferSize" value="1024"/>
>   <property name="receiveBufferSize" value="1024"/>
>   <property name="soLinger" value="10"/>
>   <property name="oobInline" value="false"/>
>   <property name="keepAlive" value="true"/>
> </bean>
>
> <bean id="ioAcceptor"
> class="org.apache.mina.integration.spring.IoAcceptorFactoryBean">
>   <property name="target" ref="acceptor"/>
>   <property name="bindings">
>     <list>
>       <bean class="org.apache.mina.integration.spring.Binding">
>         <property name="address" value=":1234"/>
>         <property name="handler" ref="TcpServerSessionHandler"/>
>         <property name="serviceConfig" ref="defaultAcceptorConfig"/>
>       </bean>
>     </list>
>   </property>
> </bean>
Looks great! One little thing, you don't have to set the serviceConfig
in your Binding anymore since you have configured the default already.

>
>  >>3. How can I request the SocketAddress from an acceptor after binding?
>>> I can't find a related method?
>>
>> AFAIK you can't. There is an isBound() method but you probably want
>> something like getBoundAdresses(), right? That should be easy enough to
>> add, at least to SocketAcceptor. I'll have a look at it.
>
> Thanx it advance!
I've just added the getBoundAddresses() method to IoAcceptor. Just grab
the latest code and it will be there.
>
>>
>> BTW, what do you and others think about adding a setDefaultConfig() to
>> the various acceptors and connectors (adding it to the IoService
>> interface will loose type safety so I don't think that's a good idea)
>> and setSessionConfig() to the various acceptor config and connector
>> config classes (like SocketAcceptorConfig)? I realize that this will
>> probably only be useful for users using Spring and other DI containers
>> but for those users it will simplify things a lot. WDYT?
>>
>
> I think it would be a good idea. Generally spoken it would be a good
> idea to implement always setter / getter pairs whereever it is
> possible and useful to simplify the things. WDYT?
I will add a JIRA issue for this. Also, with Spring 2.0 coming out very
soon we should look into simplifying the Spring integration using
Spring's custom XML configuration feature. It would make life a lot
simpler for Spring users.
>
>
-- 
Niklas Therning
Software Architect
www.spamdrain.net


Re: 3 questions regarding Mina and Spring

Posted by Michael Bauroth <Mi...@falcom.de>.
Hi Niklas,

thank you very much for your help!!! Here is my modified and optimized 
config part:

<bean id="acceptor" 
class="org.apache.mina.transport.socket.nio.SocketAcceptor"/>
<bean id="defaultAcceptorConfig" factory-bean="acceptor" 
factory-method="getDefaultConfig">
   <property name="filterChainBuilder" ref="filterChainBuilder"/>
   <property name="reuseAddress" value="false"/>
   <property name="disconnectOnUnbind" value="true"/>
</bean>
	
<bean id="sessionConfig" factory-bean="defaultAcceptorConfig" 
factory-method="getSessionConfig">
   <property name="reuseAddress" value="false"/>
   <property name="tcpNoDelay" value="true"/>
   <property name="sendBufferSize" value="1024"/>
   <property name="receiveBufferSize" value="1024"/>
   <property name="soLinger" value="10"/>
   <property name="oobInline" value="false"/>
   <property name="keepAlive" value="true"/>
</bean>

<bean id="ioAcceptor" 
class="org.apache.mina.integration.spring.IoAcceptorFactoryBean">
   <property name="target" ref="acceptor"/>
   <property name="bindings">
     <list>
       <bean class="org.apache.mina.integration.spring.Binding">
         <property name="address" value=":1234"/>
         <property name="handler" ref="TcpServerSessionHandler"/>
         <property name="serviceConfig" ref="defaultAcceptorConfig"/>
       </bean>
     </list>
   </property>
</bean>

  >>3. How can I request the SocketAddress from an acceptor after binding?
>>I can't find a related method?
> 
> AFAIK you can't. There is an isBound() method but you probably want
> something like getBoundAdresses(), right? That should be easy enough to
> add, at least to SocketAcceptor. I'll have a look at it.

Thanx it advance!

> 
> BTW, what do you and others think about adding a setDefaultConfig() to
> the various acceptors and connectors (adding it to the IoService
> interface will loose type safety so I don't think that's a good idea)
> and setSessionConfig() to the various acceptor config and connector
> config classes (like SocketAcceptorConfig)? I realize that this will
> probably only be useful for users using Spring and other DI containers
> but for those users it will simplify things a lot. WDYT?
> 

I think it would be a good idea. Generally spoken it would be a good 
idea to implement always setter / getter pairs whereever it is possible 
and useful to simplify the things. WDYT?

Regards
Michael

Re: 3 questions regarding Mina and Spring

Posted by Niklas Therning <ni...@trillian.se>.
Michael Bauroth wrote:
> Hi,
>
> I'm doing currently my first steps with the Springbased approach in
> Mina. Now I have a few questions how to convert my old code to the new
> style:
>
> 1. I'm configuring an alternate SocketAcceptor over the config file,
> which will be used for the later binding.
>
> <property name="serviceConfig">
> <bean class="org.apache.mina.transport.socket.nio.SocketAcceptorConfig">
> <property name="filterChainBuilder" ref="filterChainBuilder"/>
> <property name="reuseAddress" value="false"/>
> <property name="disconnectOnUnbind" value="false"/>
> </bean>
> </property>
Hmmm, what serviceConfig property? Are you using the
IoAcceptorFactoryBean? If you are using the IoAcceptorFactoryBean as per
the example in the Javadoc for that class the service config you create
won't become the default config for the acceptor but it will rather be
used only for a particular port you specify in the binding.

Using IoAcceptorFactoryBean the serviceConfig you specify will be used
in a call to acceptor.bind().

>
> When I check the settings after server start, I get other values than
> specified. Here is the related code:
>
> System.out.println("Starting server ...");
> System.out.println("UseDirectBuffers " +
> ByteBuffer.isUseDirectBuffers());
> System.out.println("DisconnectOnUnbind " +
> ((SocketAcceptorConfig)mAcceptor.getDefaultConfig()).isDisconnectOnUnbind());
>
> System.out.println("ReuseAddress " +
> ((SocketAcceptorConfig)mAcceptor.getDefaultConfig()).isReuseAddress());
>
> What I'm doing wrong?
As I described above the default config won't be modified when you use
IoAcceptorFactoryBean as per the example in the Javadoc. You can
configure the default config using Spring but unfortunately there is no
setDefaultConfig() in SocketAcceptor at this moment so to do this can be
rather messy:

<bean id="acceptor"
class="org.apache.mina.transport.socket.nio.SocketAcceptor"/>

<bean id="defaultAcceptorConfig" factory-bean="acceptor"
factory-method="getDefaultConfig">
<property name="filterChainBuilder" ref="filterChainBuilder"/>
<property name="reuseAddress" value="false"/>
<property name="disconnectOnUnbind" value="false"/>
</bean>


>
>
> 2. In the original code I have specified some extra settings:
>
> SocketSessionConfig tSessionConfig =
> (SocketSessionConfig)mAcceptorConfig.getSessionConfig();
>
> tSessionConfig.setReuseAddress( REUSE_ADDRESS );
> tSessionConfig.setTcpNoDelay( TCP_NO_DELAY );
> tSessionConfig.setSendBufferSize( TCP_SND_BUFFER_SIZE );
> tSessionConfig.setReceiveBufferSize( TCP_RCV_BUFFER_SIZE );
> tSessionConfig.setSoLinger( SO_LINGER );
> tSessionConfig.setOobInline( OOB_INLINE );
> tSessionConfig.setKeepAlive( KEEP_ALIVE );
>
> How can I specify these settings over the configuration xml file?
Again, at the moment there is no setSessionConfig() in
SocketSessionConfig. To do this in Spring you will again have to use a
rather messy config like the one above:

<bean id="sessionConfig" factory-bean="defaultAcceptorConfig"
factory-method="getSessionConfig">
<property name="reuseAddress" value="false"/>
<property name="tcpNoDelay" value="false"/>
...
</bean>

This example configures the session config for the defaultAcceptorConfig
bean created previously.
>
>
> 3. How can I request the SocketAddress from an acceptor after binding?
> I can't find a related method?
AFAIK you can't. There is an isBound() method but you probably want
something like getBoundAdresses(), right? That should be easy enough to
add, at least to SocketAcceptor. I'll have a look at it.

BTW, what do you and others think about adding a setDefaultConfig() to
the various acceptors and connectors (adding it to the IoService
interface will loose type safety so I don't think that's a good idea)
and setSessionConfig() to the various acceptor config and connector
config classes (like SocketAcceptorConfig)? I realize that this will
probably only be useful for users using Spring and other DI containers
but for those users it will simplify things a lot. WDYT?

-- 
Niklas Therning
Software Architect
www.spamdrain.net