Tuesday, March 10, 2009

JMS, quick example

before you run it, create a Queue ""scavazza_SAFImportedDestinationsscavazza_localSAFQueue_0"
(in my case, it was a SAF destination)

package com.pierre.jms;

import java.util.Date;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;


/**
 * Add these jars:
 * javax.jms_1.1.jar
 * wls-api.jar
 * 
 * @author vernetto
 *
 */
public class JMSSender {
public static void main(String[] args) throws NamingException, JMSException {
send();
}

private static void send() throws NamingException, JMSException {
String url = "t3://localhost:7001";
InitialContext ctx = JNDIHelper.getInitialContext(url);

QueueConnectionFactory qConFactory = (QueueConnectionFactory)ctx.lookup("javax/jms/QueueConnectionFactory");
QueueConnection qConnection = qConFactory.createQueueConnection();
QueueSession qSession    = qConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue)ctx.lookup("scavazza_SAFImportedDestinationsscavazza_localSAFQueue_0");
QueueSender qSender  = qSession.createSender(queue);
TextMessage msg = qSession.createTextMessage();
msg.setJMSCorrelationID("iamanidagains");
msg.setIntProperty("sonoscemo", 27);
msg.setJMSPriority(7);
qConnection.start();
msg.setText("Hello this is Pierre " + new Date());
qSender.send(msg);
qSender.setDeliveryMode(DeliveryMode.PERSISTENT);
qSender.close();
qSession.close();
qConnection.close();
System.out.println("done");
}
}



No comments: