package dk.topsecurity;
import javax.jms.*;
import javax.naming.*;
/**
* The point-to-point (PTP) messaging model enables one application to send a
* message to another. PTP messaging applications send and receive messages
* using named queues. A queue sender (producer) sends a message to a specific
* queue. A queue receiver (consumer) receives messages from a specific queue.
*/
public class JMSQueueReceiver {
public final static String QUEUE_NAME = "dk.topsecurity.queue";
public final static String JMS_FACTORY = "weblogic.jms.ConnectionFactory";
// public final static String JMS_FACTORY = "javax.jms.QueueConnectionFactory"
public static void main(String[] args)
{
try {
//requires definition of in java.naming.factory.initial,java.naming.provider.url in jndi.properties
Context ctx = new InitialContext();
QueueConnectionFactory factory =
(QueueConnectionFactory) ctx.lookup(JMS_FACTORY); //locate the Queue Connection Factory via JNDI
QueueConnection conn = factory.createQueueConnection(); //create a new Queue Connection
// Create a Queue Session, ask JMS to acknowledge the messages
// This program receives messages, so it doesn't really care.
// The session is non-transactional
QueueSession session = conn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue queue = null;
try {
queue = (Queue) ctx.lookup(QUEUE_NAME); //check if someone has already created the queue
}
catch (NameNotFoundException exc) {
queue = session.createQueue("HelloQueue"); //if not, create a new Queue and store it in the JNDI directory
ctx.bind(QUEUE_NAME, queue);
}
QueueReceiver receiver = session.createReceiver(queue); //create a QueueReceiver to receive messages
conn.start(); //tell the Queue Connection you are ready to interact with the message service
for (;;) { //receive next message and echo message contents to console
System.out.println(((TextMessage) receiver.receive()).getText());
}
}
catch (Exception exc)
{
exc.printStackTrace();
}
}
}