You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Dean H <de...@hotmail.com> on 2010/02/02 21:02:01 UTC

MINA questions

Hello, I'm new to Camel.  I'm having some trouble getting my application
working correctly.  I've read through the http://camel.apache.org/mina.html
page several times but I'm not having much luck.  

here is my scenario:
There is a tcp server that I need to connect to on a specified port.  The
server will periodically send xml strings that I will receive.  I'd like my
camel component to block indefinitely waiting to receive a message from the
server.  When a message is received, i'd like to automatically deserialize
it into an object that I can then pass into my bean. 

I am attempting to configure my route in spring with something like this:

<route>
  <from uri="mina:tcp://serverIP:port"/>
  <unmarshal ref="jaxb">
  <bean ref="myBean" method="invoke"/>
</route>

It seems that the "from" is continually attempting to function as the server
rather than the client.  Thus, it attempts to bind to the server ip/port
(which is already allocated to the actual server process) and fails.  Can
someone provide an example configuration that accomplishes what I'm looking
to do?

-- 
View this message in context: http://old.nabble.com/MINA-questions-tp27426504p27426504.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: MINA questions

Posted by Stan Lewis <ga...@gmail.com>.
Hmmm, yeah you're right, that's not quite what you're looking for, the
polling consumer is still a consumer.  I wonder if you wouldn't be
better off using the Mina API directly to get your event driven
behavior, you could then use a Camel ProducerTemplate to send the
message to a Camel Route.  There looks to be a pretty straightforward
example of an IoHandlerAdapter here -
http://mina.apache.org/report/trunk/xref/org/apache/mina/example/netcat/

On Wed, Feb 3, 2010 at 1:21 PM, Dean H <de...@hotmail.com> wrote:
>
> I'm confused by what you mean with consumerURI and targetURI.  Is the
> consumer the "client" or the "server"?  In my scenario, I've got a server
> that is pumping out XML messages over TCP periodically.  Note, the server
> already exists in the enterprise and is not part of my solution, i'm just
> trying to connect to it.
>
> I'd like to use spring configuration for my solution.  Right now I've got
> something in place that works most of the time...but I think I'm missing
> some messages.  My current solution has a timer in place that fires every 10
> seconds.  The timer routes to a MinaEndpoint.  The MinaEndpoint has a a
> custom filter attached to it that parses the message and acts on it.
>
> The end game is to have a TCP monitor that blocks until a message is
> received from the server and then takes action.  Once a message is received,
> the TCP monitor starts up again and the process is repeated.
>
> Here's my springclient.xml file:
>
> ............................
> <bean id="minaFactory"
> class="org.apache.camel.component.mina.MinaComponent">
>  <constructor-arg index="0" ref="camel"/>
> </bean>
> <bean id="minaEndpoint" factory-bean="minaFactory"
> factory-method="createEndpoint">
>  <constructor-arg index="0" ref="minaConfig"/>
> </bean>
> <bean id="minaConfig"
> class="org.apache.camel.component.mina.MinaConfiguration">
>  <property name="protocol" value="tcp"/>
>  <property name="host" value="<<SERVER_IP>>"/>
>  <property name="port" value="<<SERVER_PORT>>"/>
>  <property name="textline" value="true"/>
>  <property name="sync" value="true"/>
>  <property name="minaLogger" value="true"/>
>  <property name="filters" ref="minaFilters"/>
> </bean>
> <bean id="minaFilters" class="java.util.ArrayList">
>  <construcutor-arg>
>    <list value-type="org.apache.mina.common.IoFilter">
>      <ref bean="myFilter">
>    </list>
>  </constructor-arg>
> </bean>
> <bean id="myFilter" class="sample.myMinaIoFilter"/>
> <camel:camelContext id="camel">
>  <camel:route>
>    <camel:from uri="timer://testTimer?fixedRate=true&period=10000"/>
>    <camel:to ref="minaEndpoint" />
>  </camel:route>
> </camel:camelContext>
> ............................
>
> --
> View this message in context: http://old.nabble.com/MINA-as-a-TCP-Client-questions-tp27426504p27440985.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>

Re: MINA questions

Posted by Dean H <de...@hotmail.com>.
I'm confused by what you mean with consumerURI and targetURI.  Is the
consumer the "client" or the "server"?  In my scenario, I've got a server
that is pumping out XML messages over TCP periodically.  Note, the server
already exists in the enterprise and is not part of my solution, i'm just
trying to connect to it.

I'd like to use spring configuration for my solution.  Right now I've got
something in place that works most of the time...but I think I'm missing
some messages.  My current solution has a timer in place that fires every 10
seconds.  The timer routes to a MinaEndpoint.  The MinaEndpoint has a a
custom filter attached to it that parses the message and acts on it.

The end game is to have a TCP monitor that blocks until a message is
received from the server and then takes action.  Once a message is received,
the TCP monitor starts up again and the process is repeated.

Here's my springclient.xml file:

............................
<bean id="minaFactory"
class="org.apache.camel.component.mina.MinaComponent">
  <constructor-arg index="0" ref="camel"/>
</bean>
<bean id="minaEndpoint" factory-bean="minaFactory"
factory-method="createEndpoint">
  <constructor-arg index="0" ref="minaConfig"/>
</bean>
<bean id="minaConfig"
class="org.apache.camel.component.mina.MinaConfiguration">
  <property name="protocol" value="tcp"/>
  <property name="host" value="<<SERVER_IP>>"/>
  <property name="port" value="<<SERVER_PORT>>"/>
  <property name="textline" value="true"/>
  <property name="sync" value="true"/>
  <property name="minaLogger" value="true"/>
  <property name="filters" ref="minaFilters"/>
</bean>
<bean id="minaFilters" class="java.util.ArrayList">
  <construcutor-arg>
    <list value-type="org.apache.mina.common.IoFilter">
      <ref bean="myFilter">
    </list>
  </constructor-arg>
</bean>
<bean id="myFilter" class="sample.myMinaIoFilter"/>
<camel:camelContext id="camel">
  <camel:route>
    <camel:from uri="timer://testTimer?fixedRate=true&amp;period=10000"/>
    <camel:to ref="minaEndpoint" />
  </camel:route>
</camel:camelContext>
............................

-- 
View this message in context: http://old.nabble.com/MINA-as-a-TCP-Client-questions-tp27426504p27440985.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: MINA questions

Posted by Stan Lewis <ga...@gmail.com>.
This looks like a good case for using a polling consumer, more info
here - http://camel.apache.org/polling-consumer.html

I've knocked together a quick unit test to be sure it's going to work
for you, here's an example:

//---------------------------------------------------------------------------
package org.apache.camel.component.mina;

import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.PollingConsumer;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;

/**
 * @version $Revision: 722878 $
 */
public class MinaPollingConsumerTest extends ContextTestSupport {
    protected String consumerUri =
"mina:tcp://localhost:8080?sync=false&minaLogger=true";
    protected String targetUri =
"mina:tcp://localhost:8081?sync=false&minaLogger=true";

    public void testMinaRoute() throws Exception {
        MockEndpoint endpoint = getMockEndpoint("mock:result");
        Object body = "Hello there!";
        endpoint.expectedBodiesReceived(body);

        // just to get the consumer running so our route can connect...
        consumer.receive(targetUri, 1000);

        template.sendBody(consumerUri, body);

        // receive the message sent by the producer template
        Exchange exchange = consumer.receive(targetUri, 1000);

        template.sendBody("direct:foo", exchange.getIn().getBody());

        assertMockEndpointsSatisfied();
    }

    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                from(consumerUri).to(targetUri);
                from("direct:foo").to("mock:result");

            }
        };
    }
}
//---------------------------------------------------------------------------

Hope that helps!

On Tue, Feb 2, 2010 at 3:02 PM, Dean H <de...@hotmail.com> wrote:
>
> Hello, I'm new to Camel.  I'm having some trouble getting my application
> working correctly.  I've read through the http://camel.apache.org/mina.html
> page several times but I'm not having much luck.
>
> here is my scenario:
> There is a tcp server that I need to connect to on a specified port.  The
> server will periodically send xml strings that I will receive.  I'd like my
> camel component to block indefinitely waiting to receive a message from the
> server.  When a message is received, i'd like to automatically deserialize
> it into an object that I can then pass into my bean.
>
> I am attempting to configure my route in spring with something like this:
>
> <route>
>  <from uri="mina:tcp://serverIP:port"/>
>  <unmarshal ref="jaxb">
>  <bean ref="myBean" method="invoke"/>
> </route>
>
> It seems that the "from" is continually attempting to function as the server
> rather than the client.  Thus, it attempts to bind to the server ip/port
> (which is already allocated to the actual server process) and fails.  Can
> someone provide an example configuration that accomplishes what I'm looking
> to do?
>
> --
> View this message in context: http://old.nabble.com/MINA-questions-tp27426504p27426504.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>