Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.jms.Connection;
- import javax.jms.Destination;
- import javax.jms.ExceptionListener;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.MessageConsumer;
- import javax.jms.MessageListener;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.apache.activemq.ActiveMQConnection;
- import org.apache.activemq.ActiveMQConnectionFactory;
- /**
- * Class Receiver is an example program that uses Java Message Service (JMS) to
- * receive messages from the Sender program.
- */
- public class Receiver
- {
- private static String user = ActiveMQConnection.DEFAULT_USER;
- private static String password = ActiveMQConnection.DEFAULT_PASSWORD;
- private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
- private static String subject = "TEST.SENDRECEIVE";
- public static void main
- (String[] args)
- throws Exception
- {
- // Create the connection.
- System.out.printf ("Creating connection ...%n");
- ActiveMQConnectionFactory connectionFactory =
- new ActiveMQConnectionFactory("tcp://localhost:61616");
- Connection connection = connectionFactory.createConnection();
- connection.setExceptionListener (new ExceptionListener()
- {
- public void onException (JMSException exc)
- {
- exc.printStackTrace (System.err);
- System.exit (1);
- }
- });
- connection.start();
- // Create the session.
- System.out.printf ("Creating session ...%n");
- Session session = connection.createSession
- (false, // No transactions
- Session.AUTO_ACKNOWLEDGE);
- Destination destination = session.createQueue (subject);
- // Create the consumer.
- System.out.printf ("Creating consumer ...%n");
- MessageConsumer consumer = session.createConsumer (destination);
- // Receive and print messages.
- System.out.printf ("Receiving messages ...%n");
- consumer.setMessageListener (new MessageListener()
- {
- public void onMessage (Message msg)
- {
- if (msg instanceof TextMessage)
- {
- try
- {
- System.out.printf ("%s%n",
- ((TextMessage)msg).getText());
- }
- catch (JMSException exc)
- {
- System.out.printf ("Could not get message text%n");
- }
- }
- else
- {
- System.out.printf ("Unknown message type%n");
- }
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment