I'm trying to write JMS Consumer using Akka-Camel.
For now, I'm using FFMQ as JMS server. I want to listen on JMS queue myqueue.
Creating JMS consumer actor is quite straightforward:
import akka.camel.{CamelMessage, Consumer}
import akka.event.Logging
class JMSConsumer extends Consumer {
val log = Logging(context.system, this)
def endpointUri = "jms:queue:myqueue"
def receive = {
case m: CamelMessage => {
log.info(s"JMS Received $m")
}
case other => {
log.info(s"JMS Received unknown message type : $other")
}
}
}
But I'm struggling with configuration of jms component.
In order to connect to JMS server, I need to specify few properties as specified in FFMQ documentation.
So far I only managed to do this by specifying parameters using some Camel internals.
import akka.actor.{Props, ActorSystem}
import akka.camel.CamelExtension
import org.apache.camel.component.jms.{JmsConfiguration, JmsComponent}
import java.util.Hashtable
import javax.naming.{InitialContext, Context}
import net.timewalker.ffmq3.FFMQConstants
import javax.jms.{Session, ConnectionFactory}
object MainApp extends App {
val system = ActorSystem("TestSystem")
def jmsConnectionFactory() : ConnectionFactory = {
val env = new Hashtable[String, String]();
env.put(Context.INITIAL_CONTEXT_FACTORY, FFMQConstants.JNDI_CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, "tcp://localhost:10002");
val context: Context = new InitialContext(env);
context.lookup(FFMQConstants.JNDI_CONNECTION_FACTORY_NAME).asInstanceOf[ConnectionFactory];
}
val jmsConfiguration = new JmsConfiguration(jmsConnectionFactory)
val jmsComponent = new JmsComponent(jmsConfiguration)
val camel = CamelExtension(system)
camel.context.addComponent("jms", jmsComponent)
val jmsConsumer = system.actorOf(Props[JMSConsumer], name="myqueueConsumer")
}
I'm wondering if this is the way how akka-camel is intended to be used.
It seems that I'm just puttings random things together, possibly going into too low-level detail when doing JNDI lookup myself?
Unfortunately, akka-camel documentation doesn't go into details here.