4

From my limited understanding of Camel's MINA2 and Netty Components I see that I can do one way communications and request-reply communications but I want to be able to send messages from the client to the server and from the server to the client asynchronously.

For example, I would like to write a simple server to echo back to any connected clients what was submitted. In addition, for any connected client I want to send the current time every thirty seconds. Is there a good way to do this? Below is a sample of what I have but don't know how to setup the part that allows the server to send messages to the connected client.

The netcat portion of the code snippets below work, and printing the current time to the server's console (via logging) works, but want to send it back to the connected client. I should note that the client is legacy software and I don't have the ability to change it. Only the server portions.

TL;DR: I want the server to send asynchronous messages to connected legacy clients that I have to support as is. Is there a built in way to do this in Camel without writing custom code? Or if not, what I would have to write?

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <bean id="echo" class="netcat.Echo" />

    <camel:errorHandler id="camelErrorHandler" type="DefaultErrorHandler" />

    <camelContext id="camelContext" errorHandlerRef="camelErrorHandler" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="quartz2://sendTime?cron=0/5+*+*+*+*+?" />
            <to uri="bean:netcat.CurrentTime?method=getCurrentTime()" />
        </route>
        <route>
            <from uri="netty:tcp://localhost:5555?textline=true" />
            <to uri="bean:echo" />
        </route>
    </camelContext>

</beans>

log4j.properties

log4j.rootLogger=INFO, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r %d [%t] %-5p %c %x - %m%n

CurrentTime.java

package netcat;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CurrentTime {

    private static final Logger LOG = LoggerFactory.getLogger(CurrentTime.class);

    private CurrentTime() {
    }

    public static String getCurrentTime() {
        String currentTime = DateTime.now().toString();
        LOG.info("Current Time is {}.", currentTime);
        return currentTime;
    }

}

Echo.java

package netcat;

import org.apache.commons.lang3.StringUtils;

public class Echo {

    public String echo(String message) {
        return StringUtils.join(new Object[] { "Echoing: ", message });
    }

}
thpatel
  • 160
  • 1
  • 10
  • 2
    I think that the problem you are experiencing is similar to the one discussed here: http://camel.465427.n5.nabble.com/Netty-two-way-communication-td5719736.html . The post is from 2012; I don't know if support has been added since or not. – Chris Finley May 12 '14 at 16:52
  • @cfinley That appears to be exactly what I am running into. I can't seem to understand the temporary solution provided by alireza. – thpatel May 12 '14 at 17:25
  • 2
    This is not possible. The MINA component in the apache camel framework is designed to be used in EIP/integration systems. Sounds like you are trying to create a more peer to peer network. The mina component only supports the in and the in out message exchange patterns. You could use mina by itself to create the server you need it is easy. – Namphibian May 13 '14 at 05:09

0 Answers0