You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by anandsk <sk...@arccorp.com> on 2010/03/10 15:08:48 UTC

single tcp connection with async requests and async responses

I can use only one thread for sending messages becuase I can have only single
TCP connection to external server. I want to be able to send multiple
requests one after the other without waiting for a response. responses need
to be processed asynchronously. any solutions for this scenario?.
I am unable to use camel 2.2 as I have package scanning problem in weblogic.
here is one possible solution but I am not sure about the life cycle of the
mina endpoint, is it possible to loose messages with this solution.

                from("file:///test/test/response") 
                .convertBodyTo(String.class).threads(1) 
               
.to("mina:tcp://localhost:6202?sync=false&textline=true&filters=#listFilters"); 
                
                from("vm:response") 
                .to("log:+++ reply++++"); 

public class MessageFilter extends IoFilterAdapter { 
    @Produce(uri = "vm:response") 
    ProducerTemplate producer; 
            @Override 
            public void messageReceived(NextFilter nextFilter, IoSession
session, 
                    Object message) throws Exception { 
                if (message instanceof String) { 
                producer.sendBody(message); 
                    } 
                
                nextFilter.messageReceived(session, message); 
            } 
        
} 

-- 
View this message in context: http://old.nabble.com/single-tcp-connection-with-async-requests-and-async-responses-tp27850137p27850137.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: single tcp connection with async requests and async responses

Posted by anandsk <sk...@arccorp.com>.
I am little bit familiar with Mina but not comfortable with managing threads
my self becuase of synchronization and thread safety issues.... as I can
have only one tcp connection to the external tcp server, I need to use
spring to initialize(establish connection) and to make sure I use the same
connection object. 


huntc wrote:
> 
> In terms of development time, this would depend on your familiarity with
> MINA I guess. My personal take is to utilise the existing Camel component.
> I think your custom component would end up looking quite simple and
> skinny.
> 
> You can load balance custom components as you can any other type of
> component of course.
> 
> 

-- 
View this message in context: http://old.nabble.com/single-tcp-connection-with-async-requests-and-async-responses-tp27850137p27858033.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: single tcp connection with async requests and async responses

Posted by huntc <hu...@mac.com>.
In terms of development time, this would depend on your familiarity with MINA
I guess. My personal take is to utilise the existing Camel component. I
think your custom component would end up looking quite simple and skinny.

You can load balance custom components as you can any other type of
component of course.

-- 
View this message in context: http://old.nabble.com/single-tcp-connection-with-async-requests-and-async-responses-tp27850137p27857929.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: single tcp connection with async requests and async responses

Posted by anandsk <sk...@arccorp.com>.
I will read the documentation on writing component, would it take longer to
write new component from scratch using Mina api. would I be able to use load
balancer component with this becuase I need to rotate Ip addresses in case
of IO timeouts or failures.


huntc wrote:
> 
> I do not recommend extending the MINA component. My recommendation is for
> you to roll your own component from scratch. This component should obtain
> the MINA endpoint and invoke its consumers and producers.
> 
> You could do this within a Processor but writing a component should
> structure your code more nicely.
> 
> More on writing components here:
> http://camel.apache.org/writing-components.html
> 
> In summary consider:
> * writing MINA encoders/decoders for your protocol (depending on the
> complexity of your protocol)
> * write a component that utilises a MINA component which in turn
> optionally utilises the encoders and decoders
> 
> That way your route code becomes something like:
> 
> from("file:///test/test/response") 
>   .convertBodyTo(String.class)
>   .to("mycomponent://localhost:6202"); 
>                 
> from("mycomponent://localhost:6202") 
>   .to("log:+++ reply++++"); 
> 
> 

-- 
View this message in context: http://old.nabble.com/single-tcp-connection-with-async-requests-and-async-responses-tp27850137p27857892.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: single tcp connection with async requests and async responses

Posted by huntc <hu...@mac.com>.
I do not recommend extending the MINA component. My recommendation is for you
to roll your own component from scratch. This component should obtain the
MINA endpoint and invoke its consumers and producers.

You could do this within a Processor but writing a component should
structure your code more nicely.

More on writing components here:
http://camel.apache.org/writing-components.html

In summary consider:
* writing MINA encoders/decoders for your protocol (depending on the
complexity of your protocol)
* write a component that utilises a MINA component which in turn optionally
utilises the encoders and decoders

That way your route code becomes something like:

from("file:///test/test/response") 
  .convertBodyTo(String.class)
  .to("mycomponent://localhost:6202"); 
                
from("mycomponent://localhost:6202") 
  .to("log:+++ reply++++"); 

-- 
View this message in context: http://old.nabble.com/single-tcp-connection-with-async-requests-and-async-responses-tp27850137p27857847.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: single tcp connection with async requests and async responses

Posted by anandsk <sk...@arccorp.com>.
so, I have to extend the existing Mina component to achieve what I want, is
there any example I can use for my scenario?. 

huntc wrote:
> 
> There's no high level method that I can think of. I find that quite often,
> when dealing with TCP endpoints as you've described, you probably need to
> think about wrapping everything up into your own component.
> 

-- 
View this message in context: http://old.nabble.com/single-tcp-connection-with-async-requests-and-async-responses-tp27850137p27857751.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: single tcp connection with async requests and async responses

Posted by huntc <hu...@mac.com>.
There's no high level method that I can think of. I find that quite often,
when dealing with TCP endpoints as you've described, you probably need to
think about wrapping everything up into your own component.
-- 
View this message in context: http://old.nabble.com/single-tcp-connection-with-async-requests-and-async-responses-tp27850137p27857707.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: single tcp connection with async requests and async responses

Posted by anandsk <sk...@arccorp.com>.
Thanks for the info. it's still little bit vague for me. can you please give
me a sample route which would do it at high level.I can use camel 2.2 if
needed. my Message header would have a correlation id so that should not be
problem. here is the sample route for the scenario I am looking for. here I
want all responses to be written to a file without requests waiting for
replies.

                from("file:///test/test/request") 
                .convertBodyTo(String.class).threads(1) 
                .to("mina:tcp://localhost:6202?sync=false&textline=true")
                .to("vm:response");

                          from("vm:response")
		.to("file:///test/test/response?fileName=response_async.txt");



huntc wrote:
> 
> Firstly if you need to guarantee just one tcp connection as per a
> connection-oriented protocol then you'll need to follow this recipe:
> 
> http://camel.apache.org/fine-grained-control-over-a-channel.html
> 
> You can send multiple requests by reading from a queue (perhaps consider
> using a 
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html
> ConcurrentLinkedQueue  i.e. poll the queue until there's nothing left on
> it.
> 
> Meanwhile you'll need another thread to obtain a polling consumer from the
> same endpoint and poll with a timeout. Of course your protocol will have
> to support some form of correlation id so that you can match your
> responses back to your requests.
> 
> That should do the trick.
> 

-- 
View this message in context: http://old.nabble.com/single-tcp-connection-with-async-requests-and-async-responses-tp27850137p27857688.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: single tcp connection with async requests and async responses

Posted by huntc <hu...@mac.com>.
Firstly if you need to guarantee just one tcp connection as per a
connection-oriented protocol then you'll need to follow this recipe:

http://camel.apache.org/fine-grained-control-over-a-channel.html

You can send multiple requests by reading from a queue (perhaps consider
using a 
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html
ConcurrentLinkedQueue  i.e. poll the queue until there's nothing left on it.

Meanwhile you'll need another thread to obtain a polling consumer from the
same endpoint and poll with a timeout. Of course your protocol will have to
support some form of correlation id so that you can match your responses
back to your requests.

That should do the trick.
-- 
View this message in context: http://old.nabble.com/single-tcp-connection-with-async-requests-and-async-responses-tp27850137p27857286.html
Sent from the Camel - Users mailing list archive at Nabble.com.