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 });
}
}