Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm trying to write a JMS Consumer using Akka-Camel.

For now, I'm using FFMQ as a JMS server. I want to listen on the JMS queue myqueue.

Creating the 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 the configuration of the JMS component.

In order to connect to JMS server, I need to specify a few properties as specified in FFMQ documentation.

So far I have 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 that akka-camel is intended to be used.

It seems that I'm just putting 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.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.