Search in sources :

Example 1 with MessageConsumer

use of javax.jms.MessageConsumer in project storm by apache.

the class JmsSpout method open.

/**
     * <code>ISpout</code> implementation.
     * <p>
     * Connects the JMS spout to the configured JMS destination
     * topic/queue.
     */
@SuppressWarnings("rawtypes")
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    if (this.jmsProvider == null) {
        throw new IllegalStateException("JMS provider has not been set.");
    }
    if (this.tupleProducer == null) {
        throw new IllegalStateException("JMS Tuple Producer has not been set.");
    }
    Integer topologyTimeout = (Integer) conf.get("topology.message.timeout.secs");
    // TODO fine a way to get the default timeout from storm, so we're not hard-coding to 30 seconds (it could change)
    topologyTimeout = topologyTimeout == null ? 30 : topologyTimeout;
    if ((topologyTimeout.intValue() * 1000) > this.recoveryPeriod) {
        LOG.warn("*** WARNING *** : " + "Recovery period (" + this.recoveryPeriod + " ms.) is less then the configured " + "'topology.message.timeout.secs' of " + topologyTimeout + " secs. This could lead to a message replay flood!");
    }
    this.queue = new LinkedBlockingQueue<Message>();
    this.toCommit = new TreeSet<JmsMessageID>();
    this.pendingMessages = new HashMap<JmsMessageID, Message>();
    this.collector = collector;
    try {
        ConnectionFactory cf = this.jmsProvider.connectionFactory();
        Destination dest = this.jmsProvider.destination();
        this.connection = cf.createConnection();
        this.session = connection.createSession(false, this.jmsAcknowledgeMode);
        MessageConsumer consumer = session.createConsumer(dest);
        consumer.setMessageListener(this);
        this.connection.start();
        if (this.isDurableSubscription() && this.recoveryPeriod > 0) {
            this.recoveryTimer = new Timer();
            this.recoveryTimer.scheduleAtFixedRate(new RecoveryTask(), 10, this.recoveryPeriod);
        }
    } catch (Exception e) {
        LOG.warn("Error creating JMS connection.", e);
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) JMSException(javax.jms.JMSException) ConnectionFactory(javax.jms.ConnectionFactory)

Example 2 with MessageConsumer

use of javax.jms.MessageConsumer in project javaee7-samples by javaee-samples.

the class ClassicMessageReceiver method receiveMessage.

public String receiveMessage() {
    String response = null;
    Connection connection = null;
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer messageConsumer = session.createConsumer(demoQueue);
        Message message = messageConsumer.receive(5000);
        response = message.getBody(String.class);
    } catch (JMSException ex) {
        ex.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
    }
    return response;
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 3 with MessageConsumer

use of javax.jms.MessageConsumer in project quickstarts by jboss-switchyard.

the class JMSClient method sendToHornetQ.

private static void sendToHornetQ(String value) throws Exception {
    HornetQMixIn hqMixIn = new HornetQMixIn(false).setUser(USER).setPassword(PASSWD);
    hqMixIn.initialize();
    try {
        Session session = hqMixIn.getJMSSession();
        final MessageProducer producer = session.createProducer(HornetQMixIn.getJMSQueue(REQUEST_NAME));
        final MessageConsumer consumer = session.createConsumer(HornetQMixIn.getJMSQueue(REPLY_NAME));
        producer.send(hqMixIn.createJMSMessage(createPayload(value)));
        System.out.println("Message sent. Waiting for reply ...");
        Message message = consumer.receive(3000);
        String reply = hqMixIn.readStringFromJMSMessage(message);
        System.out.println("REPLY: \n" + reply);
    } finally {
        hqMixIn.uninitialize();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer) HornetQMixIn(org.switchyard.component.test.mixins.hornetq.HornetQMixIn) Session(javax.jms.Session)

Example 4 with MessageConsumer

use of javax.jms.MessageConsumer in project quickstarts by jboss-switchyard.

the class JMSClient method sendToActiveMQ.

private static void sendToActiveMQ(String value) throws Exception {
    ConnectionFactory cf = new ActiveMQConnectionFactory(AMQ_USER, AMQ_PASSWD, AMQ_BROKER_URL);
    Connection conn = cf.createConnection();
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MessageProducer producer = session.createProducer(session.createQueue(REQUEST_NAME));
        final MessageConsumer consumer = session.createConsumer(session.createQueue(REPLY_NAME));
        conn.start();
        producer.send(session.createTextMessage(createPayload(value)));
        System.out.println("Message sent. Waiting for reply ...");
        Message message = consumer.receive(3000);
        String reply = ((TextMessage) message).getText();
        System.out.println("REPLY: \n" + reply);
    } finally {
        conn.close();
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 5 with MessageConsumer

use of javax.jms.MessageConsumer in project quickstarts by jboss-switchyard.

the class OrderIntakeClient method main.

/**
     * Main routing for OrderIntakeClient
     * @param args command-line args
     * @throws Exception if something goes wrong.
     */
public static void main(final String[] args) throws Exception {
    HornetQMixIn hqMixIn = new HornetQMixIn(false).setUser(USER).setPassword(PASSWD);
    hqMixIn.initialize();
    try {
        Session session = hqMixIn.getJMSSession();
        MessageProducer producer = session.createProducer(HornetQMixIn.getJMSQueue(ORDER_QUEUE_NAME));
        String orderTxt = readFileContent(args[0]);
        System.out.println("Submitting Order" + "\n" + "----------------------------\n" + orderTxt + "\n----------------------------");
        producer.send(hqMixIn.createJMSMessage(orderTxt));
        MessageConsumer consumer = session.createConsumer(HornetQMixIn.getJMSQueue(ORDERACK_QUEUE_NAME));
        System.out.println("Order submitted ... waiting for reply.");
        TextMessage reply = (TextMessage) consumer.receive(3000);
        if (reply == null) {
            System.out.println("No reply received.");
        } else {
            System.out.println("Received reply" + "\n" + "----------------------------\n" + reply.getText() + "\n----------------------------");
        }
    } finally {
        hqMixIn.uninitialize();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MessageProducer(javax.jms.MessageProducer) HornetQMixIn(org.switchyard.component.test.mixins.hornetq.HornetQMixIn) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Aggregations

MessageConsumer (javax.jms.MessageConsumer)82 Session (javax.jms.Session)53 Message (javax.jms.Message)40 MessageProducer (javax.jms.MessageProducer)40 Test (org.junit.Test)39 Connection (javax.jms.Connection)38 TextMessage (javax.jms.TextMessage)38 ConnectionFactory (javax.jms.ConnectionFactory)14 JMSException (javax.jms.JMSException)14 Destination (javax.jms.Destination)12 Queue (javax.jms.Queue)8 TemporaryQueue (javax.jms.TemporaryQueue)8 ObjectMessage (javax.jms.ObjectMessage)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 Serializable (java.io.Serializable)4 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 MessageListener (javax.jms.MessageListener)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 ArrayList (java.util.ArrayList)3