by Senthil Krishnamurhty from Aspire Systems (India) Pvt.
Ltd.
This article will give you some tips about JMS and MSMQ configurations,
with some coding examples.
JMS Example :
Step 1: Configuring JMS in Jdeveloper
OC4J
1.
Open the jms.xml file, which is in the config directory of
Jdeveloper home path.
2.
Insert the following lines if they are not present in the
file
<!-- This will create a QueueConnectionFactory and a queue. Following are the java coding samples for JMS -->
<queue-connection-factory location="jms/OracleSyndicateQueueConnectionFactory" listener-threads="3"/>
<queue name="jms/OracleSyndicateQueue" location="jms/OracleSyndicateQueue" listener-threads="3"><description>Oracle Syndication Services Queue</description>
</queue>
Step 2: Set JNDI properties
// This will lookup JMS resource in JMS Server. InitialContext jndi = null; Properties env = new Properties(); env.put(Context.SECURITY_PRINCIPAL, "admin"); env.put(Context.SECURITY_CREDENTIALS, "admin"); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.evermind.server.rmi.RMIInitialContextFactory"); env.put(Context.PROVIDER_URL, "ormi://localhost:23791"); //Specify JMS Server URL env.put("dedicated.rmicontext", Boolean.TRUE); jndi = new InitialContext(env);
Step 3: JMS Queue setup
QueueConnectionFactory conFactory = (QueueConnectionFactory) jndi.lookup("jms/OracleSyndicateQueueConnectionFactory"); // Create a JMS Queue connection QueueConnection connection = conFactory.createQueueConnection(username, password); // Create two JMS session objects pubSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); // Look up a JMS topic Queue chatQueue = (Queue) jndi.lookup("jms/OracleSyndicateQueue"); // Create a JMS publisher and subscriber publisher = pubSession.createSender(chatQueue); connection.start();
TextMessage message = pubSession.createTextMessage(); message.setText("NEWORDER"); publisher.send(message); connection.stop(); connection.close();
For receiving messages from JMS
server, javax.jms.MessageListener interface must be implemented and onMessage()
have to be overloaded which will be called automatically when any new message
has arrived to JMS server.
// Create Queue Session for subscriber QueueSession subSession = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); // Create a subscriber QueueReceiver subscriber = subSession.createReceiver(chatQueue); // Set a JMS message listener subscriber.setMessageListener(this); connection.start(); // onMessage() method. public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); System.out.println("InComing Message : "+text); }
MSMQ Example (in C#) :
1. In Control Panel,
double-click Add or Remove
Programs.
2. Click Add/Remove Windows Components.
3. In Windows
XP, select Message Queuing,
or in Windows 2000, select Message
Queueing Services, click OK, and then click Next.
4. Click Finish, and then
close the Add or Remove Programs window.
System.Messaging.MessageQueue mq;
// Here CSQueue is the queue name.
if(MessageQueue.Exists(@".\Private$\CSQueue"))
{
mq = new System.Messaging.MessageQueue(@".\Private$\CSQueue");
}
else
{
mq = MessageQueue.Create(@".\Private$\CSQueue");
}
System.Messaging.Message mm = new System.Messaging.Message();
mm.Body = "NEW_ORDER";
mm.Label = "OrderMsg";
mq.Send(mm);
System.Messaging.Message mes = mq.Receive ();
mes.Formatter = new XmlMessageFormatter (new String[] {"System.String,mscorlib"});
string m = mes.Body.ToString ();
Here we discussed some basic code samples which are needed
for configurations and handling messages in both JMS and MSMQ. You can extend
these sample coding according to your requirement for the application.