You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Muhammad Ichsan <ic...@gmail.com> on 2010/02/03 13:16:11 UTC
How to autoflush with TextLineCodecFactory
Dear All,
I'm using Mina 2 with TextLineCodeFactory as the protocol codec. As
session is opened, I need use the same connection to send many
messages. But even though the messages are sent with delay 1 second
using the same session, the remote peer has not received any of them.
UNTIL, all messages are written (session.write).
I don't want this. I want each message is sent immediately just like
simple java socket send. How to achieve this? Do I have to create my
own codec which flush each message immediately?
This is my code: http://pastebin.com/maca2e71
Thanks
--
~The best men are men who benefit to others
http://michsan.web.id
Yang berkualitas memang beda rasanya!
http://rizqi-cookies.com
Re: How to autoflush with TextLineCodecFactory
Posted by Muhammad Ichsan <ic...@gmail.com>.
On Wed, Feb 3, 2010 at 10:07 PM, Muhammad Ichsan <ic...@gmail.com> wrote:
> On Wed, Feb 3, 2010 at 10:01 PM, Emmanuel LŽcharny <el...@gmail.com> wrote:
>> Muhammad Ichsan a écrit :
>>>
>>> On Wed, Feb 3, 2010 at 9:46 PM, Emmanuel LŽcharny <el...@gmail.com>
>>> wrote:
>
>> Anyway, it's a bit complicated, and frankly, it's better not to send any
>> message here, unless you want the server to start pushing data to the client
>> (then add an executor in the chain)
>
> Thanks for this explanation.
>>>
>>>
>>> Please see this: http://pastebin.com/d18d1ea9b
>>>
>>
>> yes, so ?
>
> The server keep sending messages until all is sent (asynchronously)
> and then do the real send (the client receive the messages). The
> problem is just like the Trying.java I posted before. How to solve the
> problem?
I know I have to move the socket.write out of the sessionOpened()
method. But where? This is an acceptor.
Thanks
--
~The best men are men who benefit to others
http://michsan.web.id
Yang berkualitas memang beda rasanya!
http://rizqi-cookies.com
Re: How to autoflush with TextLineCodecFactory
Posted by Muhammad Ichsan <ic...@gmail.com>.
On Wed, Feb 3, 2010 at 10:01 PM, Emmanuel LŽcharny <el...@gmail.com> wrote:
> Muhammad Ichsan a écrit :
>>
>> On Wed, Feb 3, 2010 at 9:46 PM, Emmanuel LŽcharny <el...@gmail.com>
>> wrote:
> Anyway, it's a bit complicated, and frankly, it's better not to send any
> message here, unless you want the server to start pushing data to the client
> (then add an executor in the chain)
Thanks for this explanation.
>>
>>
>> Please see this: http://pastebin.com/d18d1ea9b
>>
>
> yes, so ?
The server keep sending messages until all is sent (asynchronously)
and then do the real send (the client receive the messages). The
problem is just like the Trying.java I posted before. How to solve the
problem?
--
~The best men are men who benefit to others
http://michsan.web.id
Yang berkualitas memang beda rasanya!
http://rizqi-cookies.com
Re: How to autoflush with TextLineCodecFactory
Posted by Emmanuel Lcharny <el...@gmail.com>.
Muhammad Ichsan a écrit :
> On Wed, Feb 3, 2010 at 9:46 PM, Emmanuel LŽcharny <el...@gmail.com> wrote:
>
>> Muhammad Ichsan a écrit :
>>
>>> But, I wonder the purpose of session in sessionOpened.
>>>
>> You may add some logic when the session is opened.
>>
> But some logics are forbidden? Like send messages using the session.
>
yes. Let's me explain why you can't send a message in the
sessionOpened() method.
When an event is generated (like the sessionOpened), it goes through a
chain of filters, then hit the application (the handler). When the
application has finished to process this event, it returns, and the
chain is unfolded, up to the place the event has been pushed to the
chain. When it's done, then the Thread is available for the next event
to be processed. The IoProcessor can kick and select one Thred to
process the event. In your case, when you write a message in yur
application, it's just pushed in a queue to be processed when there will
be an available thread. So you can't process them until you are done ith
the event processing, which explains why all the messages are sent in
one shot.
Adding an executor in the chain can help here, are it introduce some
point for other Threads to be available to process the newly added events.
Anyway, it's a bit complicated, and frankly, it's better not to send any
message here, unless you want the server to start pushing data to the
client (then add an executor in the chain)
>
>>> Also, what
>>> about if the server sends the messages first? How can I get the
>>> session?
>>>
>>>
>> You can't. The server won't create a session on a client, or it will be
>> called 'client', not server !
>>
>
> Please see this: http://pastebin.com/d18d1ea9b
>
yes, so ?
Re: How to autoflush with TextLineCodecFactory
Posted by Muhammad Ichsan <ic...@gmail.com>.
On Wed, Feb 3, 2010 at 9:46 PM, Emmanuel LŽcharny <el...@gmail.com> wrote:
> Muhammad Ichsan a écrit :
>>
>> But, I wonder the purpose of session in sessionOpened.
>
> You may add some logic when the session is opened.
But some logics are forbidden? Like send messages using the session.
>
>> Also, what
>> about if the server sends the messages first? How can I get the
>> session?
>>
>
> You can't. The server won't create a session on a client, or it will be
> called 'client', not server !
Please see this: http://pastebin.com/d18d1ea9b
>
>
Thanks
--
~The best men are men who benefit to others
http://michsan.web.id
Yang berkualitas memang beda rasanya!
http://rizqi-cookies.com
Re: How to autoflush with TextLineCodecFactory
Posted by Emmanuel Lcharny <el...@gmail.com>.
Muhammad Ichsan a écrit :
> On Wed, Feb 3, 2010 at 8:39 PM, Emmanuel LŽcharny <el...@gmail.com> wrote:
>
>> Muhammad Ichsan a écrit :
>>
>> Just move the code outside of this event, it will work perfectly. Something
>> like :
>>
>> ...
>> ConnectFuture cf = trying.createClient().connect(address);
>> cf.awaitUninterruptibly();
>> // Send many messages
>> for (int i = 0; i < 20; i++) {
>>
>
> Great! it works. Thanks.
>
> But, I wonder the purpose of session in sessionOpened.
You may add some logic when the session is opened.
> Also, what
> about if the server sends the messages first? How can I get the
> session?
>
You can't. The server won't create a session on a client, or it will be
called 'client', not server !
Re: How to autoflush with TextLineCodecFactory
Posted by Muhammad Ichsan <ic...@gmail.com>.
On Wed, Feb 3, 2010 at 8:39 PM, Emmanuel LŽcharny <el...@gmail.com> wrote:
> Muhammad Ichsan a écrit :
>>
> Just move the code outside of this event, it will work perfectly. Something
> like :
>
> ...
> ConnectFuture cf = trying.createClient().connect(address);
> cf.awaitUninterruptibly();
> // Send many messages
> for (int i = 0; i < 20; i++) {
Great! it works. Thanks.
But, I wonder the purpose of session in sessionOpened. Also, what
about if the server sends the messages first? How can I get the
session?
> ...
>
> produces :
>
> --0
> Got from client: Client.message.0
> --1
> Got from client: Client.message.1
> --2
> ...
>
--
~The best men are men who benefit to others
http://michsan.web.id
Yang berkualitas memang beda rasanya!
http://rizqi-cookies.com
Re: How to autoflush with TextLineCodecFactory
Posted by Emmanuel Lcharny <el...@gmail.com>.
Muhammad Ichsan a écrit :
> On Wed, Feb 3, 2010 at 7:31 PM, Emmanuel LŽcharny <el...@gmail.com> wrote:
>
>> Muhammad Ichsan a écrit :
>>
>>> Dear All,
>>>
>>> I'm using Mina 2 with TextLineCodeFactory as the protocol codec. As
>>>
> ^^^^^^^^^^^^^^^^^^^^^^^^
>
>> Simply add a '\n' at the end of each message. The codec you are using
>> (Text*Line*Codec) is expeciting to see *lines*, not messages.
>>
Sorry, I missed this line i your code.
Now, if you are trying to send a message in the sessionOpened() event,
it won't work.
Just move the code outside of this event, it will work perfectly.
Something like :
...
ConnectFuture cf = trying.createClient().connect(address);
cf.awaitUninterruptibly();
// Send many messages
for (int i = 0; i < 20; i++) {
...
produces :
--0
Got from client: Client.message.0
--1
Got from client: Client.message.1
--2
...
Re: How to autoflush with TextLineCodecFactory
Posted by Muhammad Ichsan <ic...@gmail.com>.
On Wed, Feb 3, 2010 at 7:31 PM, Emmanuel LŽcharny <el...@gmail.com> wrote:
> Muhammad Ichsan a écrit :
>>
>> Dear All,
>>
>> I'm using Mina 2 with TextLineCodeFactory as the protocol codec. As
^^^^^^^^^^^^^^^^^^^^^^^^
>
> Simply add a '\n' at the end of each message. The codec you are using
> (Text*Line*Codec) is expeciting to see *lines*, not messages.
>
>
It just don't work (as I thought), because I'm using mina not plain
socket. From plain socket, I do have to use '\n'. Have you seen my
code? I the codec factory already handle this.
The messages are delivered all together not one by one. Look at this result:
--0
--1
--2
--3
--4
--5
--6
--7
--8
--9
--10
--11
--12
--13
--14
--15
--16
--17
--18
--19
Got from client: Client.message.0
Got from client: Client.message.1
Got from client: Client.message.2
Got from client: Client.message.3
Got from client: Client.message.4
Got from client: Client.message.5
Got from client: Client.message.6
Got from client: Client.message.7
Got from client: Client.message.8
Got from client: Client.message.9
Got from client: Client.message.10
On server took: 2
Got from client: Client.message.11
Got from client: Client.message.12
Got from client: Client.message.13
Got from client: Client.message.14
Got from client: Client.message.15
Got from client: Client.message.16
Got from client: Client.message.17
Got from client: Client.message.18
Got from client: Client.message.19
--
~The best men are men who benefit to others
http://michsan.web.id
Yang berkualitas memang beda rasanya!
http://rizqi-cookies.com
Re: How to autoflush with TextLineCodecFactory
Posted by Emmanuel Lcharny <el...@gmail.com>.
Muhammad Ichsan a écrit :
> Dear All,
>
> I'm using Mina 2 with TextLineCodeFactory as the protocol codec. As
> session is opened, I need use the same connection to send many
> messages. But even though the messages are sent with delay 1 second
> using the same session, the remote peer has not received any of them.
> UNTIL, all messages are written (session.write).
>
> I don't want this. I want each message is sent immediately just like
> simple java socket send. How to achieve this? Do I have to create my
> own codec which flush each message immediately?
>
Simply add a '\n' at the end of each message. The codec you are using
(Text*Line*Codec) is expeciting to see *lines*, not messages.