Search in sources :

Example 71 with MessageProducer

use of javax.jms.MessageProducer in project cxf by apache.

the class PooledConnectionTempQueueTest method receiveAndRespondWithMessageIdAsCorrelationId.

public void receiveAndRespondWithMessageIdAsCorrelationId(ConnectionFactory connectionFactory, String queueName) throws JMSException {
    Connection con = connectionFactory.createConnection();
    Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(queueName));
    final javax.jms.Message inMessage = consumer.receive();
    // String requestMessageId = inMessage.getJMSMessageID();
    // System.out.println("Received message " + requestMessageId);
    final TextMessage replyMessage = session.createTextMessage("Result");
    replyMessage.setJMSCorrelationID(inMessage.getJMSMessageID());
    final MessageProducer producer = session.createProducer(inMessage.getJMSReplyTo());
    // System.out.println("Sending reply to " + inMessage.getJMSReplyTo());
    producer.send(replyMessage);
    producer.close();
    consumer.close();
    session.close();
    con.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 72 with MessageProducer

use of javax.jms.MessageProducer in project apex-malhar by apache.

the class JMSTestBase method produceMsg.

public void produceMsg(String text) throws Exception {
    // Create a ConnectionFactory
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
    // Create a Connection
    Connection connection = connectionFactory.createConnection();
    connection.start();
    // Create a Session
    Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    // Create the destination (Topic or Queue)
    Destination destination = session.createQueue("TEST.FOO");
    // Create a MessageProducer from the Session to the Topic or Queue
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    // Create a messages
    TextMessage message = session.createTextMessage(text);
    producer.send(message);
    // Clean up
    session.close();
    connection.close();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Destination(javax.jms.Destination) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 73 with MessageProducer

use of javax.jms.MessageProducer in project tomee by apache.

the class JmsTest method createSender.

@SuppressWarnings("unchecked")
private synchronized void createSender(final Connection connection, final Destination requestQueue) throws JMSException {
    Session session = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        // create request
        final Map<String, Object> request = new TreeMap<String, Object>();
        request.put("args", new Object[] { "cheese" });
        // create a new temp response queue
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final Destination responseQueue = session.createTemporaryQueue();
        // Create a request messages
        final ObjectMessage requestMessage = session.createObjectMessage();
        requestMessage.setJMSReplyTo(responseQueue);
        requestMessage.setObject((Serializable) request);
        // Send the request message
        producer = session.createProducer(requestQueue);
        producer.send(requestMessage);
        // wait for the response message
        consumer = session.createConsumer(responseQueue);
        final Message message = consumer.receive(30000);
        // verify message
        assertNotNull("Did not get a response message", message);
        assertTrue("Response message is not an ObjectMessage", message instanceof ObjectMessage);
        final ObjectMessage responseMessage = (ObjectMessage) message;
        final Serializable object = responseMessage.getObject();
        assertNotNull("Response ObjectMessage contains a null object");
        assertTrue("Response ObjectMessage does not contain an instance of Map", object instanceof Map);
        final Map<String, String> response = (Map<String, String>) object;
        // process results
        final String returnValue = response.get("return");
        assertEquals("test-cheese", returnValue);
    } finally {
        MdbUtil.close(consumer);
        MdbUtil.close(producer);
        MdbUtil.close(session);
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Serializable(java.io.Serializable) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TreeMap(java.util.TreeMap) ObjectMessage(javax.jms.ObjectMessage) MessageProducer(javax.jms.MessageProducer) Map(java.util.Map) TreeMap(java.util.TreeMap) Session(javax.jms.Session)

Example 74 with MessageProducer

use of javax.jms.MessageProducer in project tomee by apache.

the class MdbContainerTest method shouldSendMessage.

@Test
public void shouldSendMessage() throws Exception {
    assertNotNull(cf);
    final Connection connection = cf.createConnection();
    try {
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MessageProducer producer = session.createProducer(destination);
        producer.send(session.createTextMessage(TEXT));
        assertTrue(AMQXASupportTest.Listener.sync());
    } finally {
        connection.close();
    }
}
Also used : Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test) AMQXASupportTest(org.apache.openejb.activemq.AMQXASupportTest)

Example 75 with MessageProducer

use of javax.jms.MessageProducer in project tomee by apache.

the class MdbInvoker method onMessage.

public void onMessage(final Message message) {
    if (!(message instanceof ObjectMessage))
        return;
    try {
        final Session session = getSession();
        if (session == null)
            throw new IllegalStateException("Invoker has been destroyed");
        if (message == null)
            throw new NullPointerException("request message is null");
        if (!(message instanceof ObjectMessage))
            throw new IllegalArgumentException("Expected a ObjectMessage request but got a " + message.getClass().getName());
        final ObjectMessage objectMessage = (ObjectMessage) message;
        final Serializable object = objectMessage.getObject();
        if (object == null)
            throw new NullPointerException("object in ObjectMessage is null");
        if (!(object instanceof Map)) {
            if (message instanceof ObjectMessage)
                throw new IllegalArgumentException("Expected a Map contained in the ObjectMessage request but got a " + object.getClass().getName());
        }
        final Map request = (Map) object;
        final String signature = (String) request.get("method");
        final Method method = signatures.get(signature);
        final Object[] args = (Object[]) request.get("args");
        boolean exception = false;
        Object result = null;
        try {
            result = method.invoke(target, args);
        } catch (final IllegalAccessException e) {
            result = e;
            exception = true;
        } catch (final InvocationTargetException e) {
            result = e.getCause();
            if (result == null)
                result = e;
            exception = true;
        }
        MessageProducer producer = null;
        try {
            // create response
            final Map<String, Object> response = new TreeMap<String, Object>();
            if (exception) {
                response.put("exception", "true");
            }
            response.put("return", result);
            // create response message
            final ObjectMessage resMessage = session.createObjectMessage();
            resMessage.setJMSCorrelationID(objectMessage.getJMSCorrelationID());
            resMessage.setObject((Serializable) response);
            // send response message
            producer = session.createProducer(objectMessage.getJMSReplyTo());
            producer.send(resMessage);
        } catch (final Exception e) {
            e.printStackTrace();
        } finally {
            MdbUtil.close(producer);
            destroy();
        }
    } catch (final Throwable e) {
        e.printStackTrace();
    }
}
Also used : Serializable(java.io.Serializable) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) InvocationTargetException(java.lang.reflect.InvocationTargetException) JMSException(javax.jms.JMSException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectMessage(javax.jms.ObjectMessage) MessageProducer(javax.jms.MessageProducer) TreeMap(java.util.TreeMap) Map(java.util.Map) Session(javax.jms.Session)

Aggregations

MessageProducer (javax.jms.MessageProducer)184 Session (javax.jms.Session)131 TextMessage (javax.jms.TextMessage)74 Message (javax.jms.Message)64 Connection (javax.jms.Connection)60 Test (org.junit.Test)54 MessageConsumer (javax.jms.MessageConsumer)49 JMSException (javax.jms.JMSException)46 Topic (javax.jms.Topic)34 Destination (javax.jms.Destination)31 Queue (javax.jms.Queue)21 ConnectionFactory (javax.jms.ConnectionFactory)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 ObjectMessage (javax.jms.ObjectMessage)10 ModelNode (org.jboss.dmr.ModelNode)10 BytesMessage (javax.jms.BytesMessage)9 QueueSession (javax.jms.QueueSession)9 TemporaryQueue (javax.jms.TemporaryQueue)9 QueueConnection (javax.jms.QueueConnection)7 InitialContext (javax.naming.InitialContext)7