Example usage for javax.jms MessageConsumer setMessageListener

List of usage examples for javax.jms MessageConsumer setMessageListener

Introduction

In this page you can find the example usage for javax.jms MessageConsumer setMessageListener.

Prototype

void setMessageListener(MessageListener listener) throws JMSException;

Source Link

Document

Sets the MessageConsumer 's MessageListener .

Usage

From source file:org.firstopen.custom.view.EventMonitorBean.java

public EventMonitorBean(String name) throws InfrastructureException {
    if (System.getSecurityManager() == null) {

        System.setSecurityManager(new SecurityManager());
    }//from ww  w.ja  v  a  2 s .c o m

    JMSUtil.createQueue(MONITOR_NAME);
    connection = JMSUtil.getQueueConnection();
    try {
        queueSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        MessageConsumer receiver = queueSession.createConsumer(JMSUtil.getQueue(MONITOR_NAME));

        receiver.setMessageListener(this);

        connection.start();
    } catch (JMSException e) {

        log.error("unable to start listener on Queue");
        throw new InfrastructureException(e);
    }
}

From source file:TransactedTalk.java

/** Create JMS client for sending and receiving messages. */
private void talker(String broker, String username, String password, String rQueue, String sQueue) {
    // Create a connection.
    try {/*from  w  w  w  .ja v  a 2 s.c om*/
        javax.jms.ConnectionFactory factory;
        factory = new ActiveMQConnectionFactory(username, password, broker);
        connect = factory.createConnection(username, password);
        // We want to be able up commit/rollback messages sent,
        // but not affect messages received.  Therefore, we need two sessions.
        sendSession = connect.createSession(true, javax.jms.Session.AUTO_ACKNOWLEDGE);
        receiveSession = connect.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
    } catch (javax.jms.JMSException jmse) {
        System.err.println("error: Cannot connect to Broker - " + broker);
        jmse.printStackTrace();
        System.exit(1);
    }

    // Create Sender and Receiver 'Talk' queues
    try {
        if (sQueue != null) {
            javax.jms.Queue sendQueue = sendSession.createQueue(sQueue);
            sender = sendSession.createProducer(sendQueue);
        }
        if (rQueue != null) {
            javax.jms.Queue receiveQueue = receiveSession.createQueue(rQueue);
            javax.jms.MessageConsumer qReceiver = receiveSession.createConsumer(receiveQueue);
            qReceiver.setMessageListener(this);
            // Now that 'receive' setup is complete, start the Connection
            connect.start();
        }
    } catch (javax.jms.JMSException jmse) {
        jmse.printStackTrace();
        exit();
    }

    try {
        if (rQueue != null)
            System.out.println("");
        else
            System.out.println("\nNo receiving queue specified.\n");

        // Read all standard input and send it as a message.
        java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

        if (sQueue != null) {
            System.out.println("TransactedTalk application:");
            System.out.println("===========================");
            System.out.println("The application user " + username + " connects to the broker at "
                    + DEFAULT_BROKER_NAME + ".");
            System.out.println("The application will stage messages to " + sQueue
                    + " until you either commit them or roll them back.");
            System.out.println("The application receives messages on " + rQueue
                    + " to consume any committed messages sent there.\n");
            System.out.println("1. Enter text to send and then press Enter to stage the message.");
            System.out.println("2. Add a few messages to the transaction batch.");
            System.out.println("3. Then, either:");
            System.out.println(
                    "     o Enter the text 'COMMIT', and press Enter to send all the staged messages.");
            System.out.println(
                    "     o Enter the text 'CANCEL', and press Enter to drop the staged messages waiting to be sent.");
        } else
            System.out.println("\nPress CTRL-C to exit.\n");

        while (true) {
            String s = stdin.readLine();

            if (s == null)
                exit();
            else if (s.trim().equals("CANCEL")) {
                // Rollback the messages. A new transaction is implicitly
                // started for following messages.
                System.out.print("Cancelling messages...");
                sendSession.rollback();
                System.out.println("Staged messages have been cleared.");
            } else if (s.length() > 0 && sQueue != null) {
                javax.jms.TextMessage msg = sendSession.createTextMessage();
                msg.setText(username + ": " + s);
                // Queues usually are used for PERSISTENT messages.
                // Hold messages for 30 minutes (1,800,000 millisecs).
                sender.send(msg, javax.jms.DeliveryMode.PERSISTENT, javax.jms.Message.DEFAULT_PRIORITY,
                        MESSAGE_LIFESPAN);
                // See if we should send the messages
                if (s.trim().equals("COMMIT")) {
                    // Commit (send) the messages. A new transaction is
                    // implicitly  started for following messages.
                    System.out.print("Committing messages...");
                    sendSession.commit();
                    System.out.println("Staged messages have all been sent.");
                }
            }
        }
    } catch (java.io.IOException ioe) {
        ioe.printStackTrace();
    } catch (javax.jms.JMSException jmse) {
        jmse.printStackTrace();
    }
    // Close the connection.
    exit();
}

From source file:org.mot.common.mq.ActiveMQFactory.java

/**
 * Use this method to create a message consumer object. 
 * //from  ww  w  . jav a2 s.c  o  m
 * @param source - provide a source as destination object 
 * @param messageProcessor - provide a message listener interface for the message processing
 */
public void createMessageConsumer(Destination source, MessageListener messageProcessor) {
    try {
        MessageConsumer msg = session.createConsumer(source);
        msg.setMessageListener(messageProcessor);
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.facetime.communication.activemq.AmqConsumer.java

public synchronized void closeConsumer(Destination destination) throws JMSException {
    MessageConsumer consumer = consumers.get(destination);
    if (consumer != null) {
        consumers.remove(destination);/*from w w w  .  ja va 2s.  c  om*/
        consumer.setMessageListener(null);
        if (consumer instanceof MessageAvailableConsumer) {
            ((MessageAvailableConsumer) consumer).setAvailableListener(null);
        }
        consumer.close();
    }
}

From source file:SelectorTalk.java

/** Create JMS client for sending and receiving messages. */
private void talker(String broker, String username, String password, String rQueue, String sQueue,
        String selection) {/*  w w w  .jav  a  2  s . c  o m*/
    // Create a connection.
    try {
        javax.jms.ConnectionFactory factory;
        factory = new ActiveMQConnectionFactory(username, password, broker);
        connect = factory.createConnection(username, password);
        sendSession = connect.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        receiveSession = connect.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
    } catch (javax.jms.JMSException jmse) {
        System.err.println("error: Cannot connect to Broker - " + broker);
        jmse.printStackTrace();
        System.exit(1);
    }

    // Create Sender and Receiver 'Talk' queues
    try {
        if (sQueue != null) {
            javax.jms.Queue sendQueue = sendSession.createQueue(sQueue);
            sender = sendSession.createProducer(sendQueue);
        }
        if (rQueue != null) {
            //NOTE: the Queue Receiver is set up with the Message Selector:
            javax.jms.Queue receiveQueue = receiveSession.createQueue(rQueue);
            javax.jms.MessageConsumer qReceiver = receiveSession.createConsumer(receiveQueue,
                    PROPERTY_NAME + " = \'" + selection + "\'");
            qReceiver.setMessageListener(this);
            connect.start();
        }
    } catch (javax.jms.JMSException jmse) {
        jmse.printStackTrace();
        exit();
    }

    try {
        if (rQueue != null)
            System.out.println("");
        else
            System.out.println("\nNo receiving queue specified.\n");

        // Read all standard input and send it as a message.
        java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
        if (sQueue != null)
            System.out.println("SelectorTalk application:\n" + "=========================\n"
                    + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME
                    + ".\n" + "The application will send messages with " + PROPERTY_NAME + " set to "
                    + selection + " to the " + sQueue + " queue.\n"
                    + "The application creates a receiver on the " + rQueue
                    + " queue selecting only messages where " + PROPERTY_NAME + " is " + selection + ".\n\n"

                    + "Type some text, and then press Enter to publish it as a TextMesssage from " + username
                    + ".\n");
        else
            System.out.println("\nPress CTRL-C to exit.\n");

        while (true) {
            String s = stdin.readLine();

            if (s == null)
                exit();
            else if (s.length() > 0 && sQueue != null) {
                javax.jms.TextMessage msg = sendSession.createTextMessage();
                msg.setText(username + ": " + s);
                // NOTE: here we set the property for each sent message.
                msg.setStringProperty(PROPERTY_NAME, selection);
                sender.send(msg, javax.jms.DeliveryMode.PERSISTENT, javax.jms.Message.DEFAULT_PRIORITY,
                        MESSAGE_LIFESPAN);
            }
        }
    } catch (java.io.IOException ioe) {
        ioe.printStackTrace();
    } catch (javax.jms.JMSException jmse) {
        jmse.printStackTrace();
    }
    // Close the connection.
    exit();
}

From source file:org.mot.common.mq.ActiveMQFactory.java

/**
 * Use this method to create a message consumer object. 
 * /* w ww  . j a v  a 2  s  . c om*/
 * @param source - provide a source as destination object 
 * @param messageProcessor - provide a message listener interface for the message processing
 * @param symbol - provide the symbol
 */
public void createMessageConsumer(final Destination source, final MessageListener messageProcessor,
        final String symbol) {

    try {
        MessageConsumer msg = session.createConsumer(source, "Symbol = '" + symbol + "'");
        msg.setMessageListener(messageProcessor);

    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.openengsb.opencit.core.projectmanager.internal.ProjectManagerImpl.java

private void startNotificationListener(Project project, DependencyProperties dependency) {
    try {//from w  ww  .j a  va 2  s. c o m
        UpdateTopicListener listener = new UpdateTopicListener();
        listener.setProject(project);
        listener.setDependency(dependency.getId());
        listener.setProjectManager(this);

        Destination topic = session.createTopic(dependency.getTopic());
        MessageConsumer consumer = session.createConsumer(topic);
        consumer.setMessageListener(listener);
    } catch (JMSException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.servicemix.wsn.jms.JmsSubscription.java

protected void start() throws SubscribeCreationFailedFault {
    try {/*  www .j  a  v a2  s  .co  m*/

        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        if (session == null) {
            System.out.println("jmsTopic:" + jmsTopic);
            MessageConsumer consumer = session.createConsumer(jmsTopic);
            consumer.setMessageListener(this);
        }
    } catch (JMSException e) {
        SubscribeCreationFailedFaultType fault = new SubscribeCreationFailedFaultType();
        throw new SubscribeCreationFailedFault("Error starting subscription", fault, e);
    }
}

From source file:org.mot.common.mq.ActiveMQFactory.java

/**
 * @param source//  w  w  w. j  a  va 2  s.  c o  m
 * @param messageProcessor
 * @param symbol
 * @param currency
 */
public void createMessageConsumer(final Destination source, final MessageListener messageProcessor,
        final String symbol, final String currency) {

    try {
        MessageConsumer msg = session.createConsumer(source,
                "Symbol = '" + symbol + "' AND Currency ='" + currency + "'");
        msg.setMessageListener(messageProcessor);

    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.apache.servicemix.wsn.jms.JmsSubscription.java

@Override
protected void resume() throws ResumeFailedFault {
    if (session != null) {
        ResumeFailedFaultType fault = new ResumeFailedFaultType();
        throw new ResumeFailedFault("Subscription is already running", fault);
    } else {/*  ww w  . j a v a 2 s . c  o  m*/
        try {
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageConsumer consumer = session.createConsumer(jmsTopic);
            consumer.setMessageListener(this);
        } catch (JMSException e) {
            ResumeFailedFaultType fault = new ResumeFailedFaultType();
            throw new ResumeFailedFault("Error resuming subscription", fault, e);
        }
    }
}