You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Nitin <ni...@integramicro.com> on 2015/08/26 12:14:36 UTC

MINA: Some messages writen to session are not going

Dear All, 
 
      We have developed one server using Apache MINA V2.0.9 where multiple
vendors connect and send the messages which needs to be processed and then
response have to be sent back to vendor. Currently we have put a strategy
that vendor has to send one message per connection and after getting
response or timeout vendor has to close the connection.
      
      We also have persistance connection open from our MINA based server
with third party server whcih is tcp/ip channel and for each vendor request
we get we have to send the received message to this third party server. Past
3-4 days we have observed that some of the received vendor messages when we
try to send on the third party server over persistance connection they are
actually not sent even after calling iosession.write(object message) method.
This operation is asynchronous; IoHandler.messageSent(IoSession, Object)
will be invoked when the message is actually sent to remote peer. In our
observation we found that the messageSent was not called for those missing
messages.
      
      We have our own ProtocolCodecFactory implemented is used for
endocing/decoding messages from our server to third party server.
      
      In the IoSession Interface documentation it is mentioned that 
      
      Thread Safety
      
IoSession is thread-safe. But please note that performing more than one
write(Object) calls at the same time will cause the
IoFilter.filterWrite(IoFilter.NextFilter,IoSession,WriteRequest) to be
executed simultaneously, and therefore you have to make sure the IoFilter
implementations you're using are thread-safe, too. 
 
 Does above statement means that I have to make sure my encoder should be
thread-safe. 
 
Below is my Protocol Encoder class
 
public class DefaultMyMsgEncoder extends ProtocolEncoderAdapter
{
    public DefaultMyMsgEncoder()
    {
    }
 
    public void encode(IoSession session, Object msg, ProtocolEncoderOutput
out) throws Exception
    {
        
            MyMsg myMsg = (MyMsg) msg;
            IoBuffer buffer =
IoBuffer.allocate(myMsg.getLengthBytes().length+myMsg.getMsgBytes().length
);
         buffer.put(myMsg.getLengthBytes());
         buffer.put(myMsg.getMsgBytes());
         buffer.flip();
         out.write(buffer);
        
    }
}
 

Thanks in advance. 
 
Thanks And Regards,
Nitin Phuria


Re: MINA: Some messages writen to session are not going

Posted by Emmanuel Lécharny <el...@gmail.com>.
Le 27/08/15 09:58, Nitin a écrit :
> Dear Emmanuel,
>
> 	I have no other own thread as well as no executorFilter filter added
> in the chain but still we have issues with the message drop.

So, for UDP messages, things are quite simple : a message can be fully
sent, or not. There is no such things as partial UDP messages being
sent. Bottom line, if a message get submitted many times to a client,
that means something wrong is done on your application.

I can't tell why without your code.


RE: MINA: Some messages writen to session are not going

Posted by Nitin <ni...@integramicro.com>.
Dear Emmanuel,

	I have no other own thread as well as no executorFilter filter added
in the chain but still we have issues with the message drop.

Thanks And Regards,
Nitin Phuria

-----Original Message-----
From: Emmanuel Lécharny [mailto:elecharny@gmail.com] 
Sent: Thursday, August 27, 2015 12:53 PM
To: users@mina.apache.org
Subject: Re: MINA: Some messages writen to session are not going

Le 27/08/15 06:59, Nitin a écrit :
> Dear Emmanuel,
>
> 	Thanks for the reply. As you mentioned in your reply that
>
> "It's possible that one message override another one before it's 
> written into the buffer. Try to add a lock associated with the 
> session, and protect the section here you create the buffer."

The key here is that a session is always executed by one single thread,
unless you have added an executorFilter in the chain, or you have your own
threads calling session.write().

Do you have either an executorFilter or your own thread ?


Re: MINA: Some messages writen to session are not going

Posted by Emmanuel Lécharny <el...@gmail.com>.
Le 27/08/15 06:59, Nitin a écrit :
> Dear Emmanuel,
>
> 	Thanks for the reply. As you mentioned in your reply that 
>
> "It's possible that one message override another one before it's written
> into the buffer. Try to add a lock associated with the session, and protect
> the section here you create the buffer."

The key here is that a session is always executed by one single thread,
unless you have added an executorFilter in the chain, or you have your
own threads calling session.write().

Do you have either an executorFilter or your own thread ?


RE: MINA: Some messages writen to session are not going

Posted by Nitin <ni...@integramicro.com>.
Dear Emmanuel,

	Thanks for the reply. As you mentioned in your reply that 

"It's possible that one message override another one before it's written
into the buffer. Try to add a lock associated with the session, and protect
the section here you create the buffer."

When I checked the built in MINA filters classes as PrefixedStringEncoder,
TextLineEncoder, ObjectSerializationEncoder I didn't see any lock associated
with the session is done. Does this mean that these filters have issue of
Thread-Safety and may create problem similar to what I am facing now.

I will change the code in my Encoder as below where I keep
ProtocolEncoderOutput out object in synchronized block. I hope this should
be fine. Kindly confirm.
 
public class DefaultMyMsgEncoder extends ProtocolEncoderAdapter 
{
    public DefaultMyMsgEncoder()
    {
    }
 
    public void encode(IoSession session, Object msg, ProtocolEncoderOutput
out) throws Exception
    {
         MyMsg myMsg = (MyMsg) msg;
         IoBuffer buffer =
IoBuffer.allocate(myMsg.getLengthBytes().length+myMsg.getMsgBytes().length);
         buffer.put(myMsg.getLengthBytes());
         buffer.put(myMsg.getMsgBytes());
         buffer.flip();
         synchronized (out)
         {
         out.write(buffer);
         }
    }
}

Thanks And Regards,
Nitin Phuria


-----Original Message-----
From: Emmanuel Lécharny [mailto:elecharny@gmail.com] 
Sent: Wednesday, August 26, 2015 5:46 PM
To: users@mina.apache.org
Subject: Re: MINA: Some messages writen to session are not going

Le 26/08/15 12:14, Nitin a écrit :
> Dear All,
>  
>       We have developed one server using Apache MINA V2.0.9 where 
> multiple vendors connect and send the messages which needs to be 
> processed and then response have to be sent back to vendor. Currently 
> we have put a strategy that vendor has to send one message per 
> connection and after getting response or timeout vendor has to close the
connection.
>       
>       We also have persistance connection open from our MINA based 
> server with third party server whcih is tcp/ip channel and for each 
> vendor request we get we have to send the received message to this 
> third party server. Past
> 3-4 days we have observed that some of the received vendor messages 
> when we try to send on the third party server over persistance 
> connection they are actually not sent even after calling
iosession.write(object message) method.
> This operation is asynchronous; IoHandler.messageSent(IoSession, 
> Object) will be invoked when the message is actually sent to remote 
> peer. In our observation we found that the messageSent was not called 
> for those missing messages.
The messageSent event will be generated when the message has actually been
written fully in the socket. You have no control on this event, you can only
react on such an event in your handler.


>       
>       We have our own ProtocolCodecFactory implemented is used for 
> endocing/decoding messages from our server to third party server.
>       
>       In the IoSession Interface documentation it is mentioned that
>       
>       Thread Safety
>       
> IoSession is thread-safe. But please note that performing more than 
> one
> write(Object) calls at the same time will cause the
> IoFilter.filterWrite(IoFilter.NextFilter,IoSession,WriteRequest) to be 
> executed simultaneously, and therefore you have to make sure the 
> IoFilter implementations you're using are thread-safe, too.
>  
>  Does above statement means that I have to make sure my encoder should 
> be thread-safe.

Yes.
>  
> Below is my Protocol Encoder class
>  
> public class DefaultMyMsgEncoder extends ProtocolEncoderAdapter {
>     public DefaultMyMsgEncoder()
>     {
>     }
>  
>     public void encode(IoSession session, Object msg, 
> ProtocolEncoderOutput
> out) throws Exception
>     {
>         
>             MyMsg myMsg = (MyMsg) msg;
>             IoBuffer buffer =
> IoBuffer.allocate(myMsg.getLengthBytes().length+myMsg.getMsgBytes().le
> ngth
> );
>          buffer.put(myMsg.getLengthBytes());
>          buffer.put(myMsg.getMsgBytes());
>          buffer.flip();
>          out.write(buffer);
>         
>     }
> }

It's possible that one message override another one before it's written into
the buffer. Try to add a lock associated with the session, and protect the
section here you create the buffer.




Re: MINA: Some messages writen to session are not going

Posted by Emmanuel Lécharny <el...@gmail.com>.
Le 26/08/15 12:14, Nitin a écrit :
> Dear All, 
>  
>       We have developed one server using Apache MINA V2.0.9 where multiple
> vendors connect and send the messages which needs to be processed and then
> response have to be sent back to vendor. Currently we have put a strategy
> that vendor has to send one message per connection and after getting
> response or timeout vendor has to close the connection.
>       
>       We also have persistance connection open from our MINA based server
> with third party server whcih is tcp/ip channel and for each vendor request
> we get we have to send the received message to this third party server. Past
> 3-4 days we have observed that some of the received vendor messages when we
> try to send on the third party server over persistance connection they are
> actually not sent even after calling iosession.write(object message) method.
> This operation is asynchronous; IoHandler.messageSent(IoSession, Object)
> will be invoked when the message is actually sent to remote peer. In our
> observation we found that the messageSent was not called for those missing
> messages.
The messageSent event will be generated when the message has actually
been written fully in the socket. You have no control on this event, you
can only react on such an event in your handler.


>       
>       We have our own ProtocolCodecFactory implemented is used for
> endocing/decoding messages from our server to third party server.
>       
>       In the IoSession Interface documentation it is mentioned that 
>       
>       Thread Safety
>       
> IoSession is thread-safe. But please note that performing more than one
> write(Object) calls at the same time will cause the
> IoFilter.filterWrite(IoFilter.NextFilter,IoSession,WriteRequest) to be
> executed simultaneously, and therefore you have to make sure the IoFilter
> implementations you're using are thread-safe, too. 
>  
>  Does above statement means that I have to make sure my encoder should be
> thread-safe. 

Yes.
>  
> Below is my Protocol Encoder class
>  
> public class DefaultMyMsgEncoder extends ProtocolEncoderAdapter
> {
>     public DefaultMyMsgEncoder()
>     {
>     }
>  
>     public void encode(IoSession session, Object msg, ProtocolEncoderOutput
> out) throws Exception
>     {
>         
>             MyMsg myMsg = (MyMsg) msg;
>             IoBuffer buffer =
> IoBuffer.allocate(myMsg.getLengthBytes().length+myMsg.getMsgBytes().length
> );
>          buffer.put(myMsg.getLengthBytes());
>          buffer.put(myMsg.getMsgBytes());
>          buffer.flip();
>          out.write(buffer);
>         
>     }
> }

It's possible that one message override another one before it's written
into the buffer. Try to add a lock associated with the session, and
protect the section here you create the buffer.