In a small departure from our normal Java oriented examples, this post shows how to send a JMS message from Scala. It is basically a port of the simple JMS client found in this post.
This example is written to run against WebLogic Server 10.3.2. Details of the necessary WebLogic Server configuration necessary to run this code can be found in that same post referred to above. You can also find a C# JMS client here.
Update: You can grab this code from our Subversion repository:
svn checkout https://www.samplecode.oracle.com/svn/jmsclients/trunk
Here is the code:
1 import java.util.{Hashtable => JHashtable} 2 import javax.naming._ 3 import javax.jms._ 4 5 object SimpleJMSClient { 6 7 val DEFAULT_QCF_NAME = "jms/MarksConnectionFactory" 8 val DEFAULT_QUEUE_NAME = "jms/MarksQueue" 9 val DEFAULT_URL = "t3://localhost:7101" 10 val DEFAULT_USER = "weblogic" 11 val DEFAULT_PASSWORD = "weblogic" 12 13 def sendMessage(theMessage: String) { 14 sendMessage( 15 url = DEFAULT_URL, 16 user = DEFAULT_USER, 17 password = DEFAULT_PASSWORD, 18 cf = DEFAULT_QCF_NAME, 19 queue = DEFAULT_QUEUE_NAME, 20 messageText = theMessage) 21 } 22 23 def sendMessage(url : String, user : String, password : String, 24 cf : String, queue : String, messageText : String) { 25 // create InitialContext 26 val properties = new JHashtable[String, String] 27 properties.put(Context.INITIAL_CONTEXT_FACTORY, 28 "weblogic.jndi.WLInitialContextFactory") 29 properties.put(Context.PROVIDER_URL, url) 30 properties.put(Context.SECURITY_PRINCIPAL, user) 31 properties.put(Context.SECURITY_CREDENTIALS, password) 32 33 try { 34 val ctx = new InitialContext(properties) 35 println("Got InitialContext " + ctx.toString()) 36 37 // create QueueConnectionFactory 38 val qcf = (ctx.lookup(cf)).asInstanceOf[QueueConnectionFactory] 39 println("Got QueueConnectionFactory " + qcf.toString()) 40 41 // create QueueConnection 42 val qc = qcf.createQueueConnection() 43 println("Got QueueConnection " + qc.toString()) 44 45 // create QueueSession 46 val qsess = qc.createQueueSession(false, 0) 47 println("Got QueueSession " + qsess.toString()) 48 49 // lookup Queue 50 val q = (ctx.lookup(queue)).asInstanceOf[Queue] 51 println("Got Queue " + q.toString()) 52 53 // create QueueSender 54 val qsndr = qsess.createSender(q) 55 println("Got QueueSender " + qsndr.toString()) 56 57 // create TextMessage 58 val message = qsess.createTextMessage() 59 println("Got TextMessage " + message.toString()) 60 61 // set message text in TextMessage 62 message.setText(messageText) 63 println("Set text in TextMessage " + message.toString()) 64 65 // send message 66 qsndr.send(message) 67 println("Sent message ") 68 69 } catch { 70 case ne : NamingException => 71 ne.printStackTrace(System.err) 72 System.exit(0) 73 case jmse : JMSException => 74 jmse.printStackTrace(System.err) 75 System.exit(0) 76 case _ => 77 println("Got other/unexpected exception") 78 System.exit(0) 79 } 80 } 81 82 def main(args: Array[String]) = { 83 sendMessage( 84 theMessage = "hello from Scala sendMessage() with 1 arg" 85 ) 86 sendMessage( 87 url = "t3://localhost:7101", 88 user = "weblogic", 89 password = "weblogic", 90 cf = "jms/MarksConnectionFactory", 91 queue = "jms/MarksQueue", 92 messageText = "hello from Scala sendMessage() with 6 args" 93 ) 94 } 95 96 }
Note that on line 26, we need to say JHashtable[String, String] to make this (old) Java API work in Scala.
To compile and run this code, you will need to put the wlthint3client.jar on your classpath, as shown below:
scala -classpath ~/Oracle/Middleware/wlserver_10.3/server/lib/wlthint3client.jar:. SimpleJMSClient
The output should look a little like this (if everything works!):
Got InitialContext javax.naming.InitialContext@435db13f
Got QueueConnectionFactory weblogic.jms.client.JMSConnectionFactory@21ed5459
Got QueueConnection weblogic.jms.client.WLConnectionImpl@41759d12
Got QueueSession weblogic.jms.client.WLSessionImpl@491cc367
Got Queue Module1!MarksQueue
Got QueueSender weblogic.jms.client.WLProducerImpl@72813bc1
Got TextMessage TextMessage[null, null]
Set text in TextMessage TextMessage[null, hello from sendMessage() with 1 arg]
Sent message
Got InitialContext javax.naming.InitialContext@5c2bfdff
Got QueueConnectionFactory weblogic.jms.client.JMSConnectionFactory@465ff916
Got QueueConnection weblogic.jms.client.WLConnectionImpl@5374a6e2
Got QueueSession weblogic.jms.client.WLSessionImpl@f786a3c
Got Queue Module1!MarksQueue
Got QueueSender weblogic.jms.client.WLProducerImpl@689e8c34
Got TextMessage TextMessage[null, null]
Set text in TextMessage TextMessage[null, hello from sendMessage() with 6 args]
Sent message
And, as shown in the referenced post, you can go into the WebLogic Server console and read the messages!
This code shows how we can create a nice, simple API to send a JMS message. Combined with the native XML support in Scala, this could provide particularly clean and compact code for sending XML messages with changing fields, as we might want to do when writing load simulators.
This would be useful for Business Activity Monitoring or Oracle Service Bus, where we may want to send simliar messages repeatedly, with only small changes to some fields, e.g. incrementing order numbers or updating status fields.
Note: This content is also posted here in a slightly different treatment, for a different audience.