Basic MDB with Scala, Sbt and JEE 6
As an example let’s take a simple MDB that will consome operation commands like add and substract and my be asked to replay by the result
I- Exchange APIsealed trait TOperation case class Add(x:Int) extends TOperation case class Sub(x:Int) extends TOperation case class Sum extends TOperation case class Count(x:Int) extends TOperationII- The listener
The object
object MDbListener {
var count = 0
val L = Logger.getLogger(classOf[MDbListener])
}
The class
@MessageDriven(
messageListenerInterface=classOf[MessageListener],
mappedName = "jms/toMdb",
activationConfig = Array(
new ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
new ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")))
class MDbListener extends MessageListener {
import MDbListener._
override def onMessage(message: Message) {
L.debug("Message Received")
message match {
case o: ObjectMessage => {
try {
val obj = o.getObject()
obj match {
case action: TOperation => handle(action)
case _ => L.warn("Unkownen msg")
}
} catch {
case ex: Exception => L.error(">>> Exception", ex)
}
}
case _ => L.warn("unknown msg")
}
}
def handle(op: TOperation ) = op match {/* TODO */
}
} Nothing special here but be careful to not forget adding messageListenerInterface=classOf[MessageListener] on MessageDriven annotation. This is because of ScalaObject super class and that’s all for the listener.
III- The senderThe handle method will do mathematical operations and can be requested to send the sum to another JMS Queue.
def handle(op: TOperation ) = op match {
case Add(x) => count +=x
case Sub(x) => count -=x
case s:Sum => sendJMSMessageToTt( Count (count))
case c:Count => L.warn ("Wrong message destination!")
}Very easy with pattern matching!
The sendJmsMessageToIt method can be done like the following
@Resource(mappedName = "jms/Rep")
var queue :Queue=_
@Resource(mappedName = "jms/RepFactory")
var factory:ConnectionFactory=_
private [ws ] def sendJMSMessageToTt( messageData:Count) {
def createJMSMessageForjmsTt( session:Session, messageData:Count):Message={
val om = session.createObjectMessage()
om.setObject(messageData)
om
}
def tryWith[A<%{ def close()}](ac :A)(f: A=> Unit)
{
try {f} finally { ac.close }
}
tryWith(factory.createConnection()){
connection => tryWith(connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
session => tryWith( session createProducer queue ){
messageProducer => messageProducer.send(createJMSMessageForjmsTt(session, messageData))
}
}
}
}
Here we used an old trick to manage resources : http://ouertani.com/2010/12/scala-try-with-resources/
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




