Search in sources :

Example 71 with MessageConsumer

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

the class SimpleJmsTest method testProxy.

public void testProxy() throws Exception {
    // create reciever object
    final TestObject testObject = new TestObject("foo");
    final MdbInvoker mdbInvoker = new MdbInvoker(connectionFactory, testObject);
    // Create a Session
    final Connection connection = connectionFactory.createConnection();
    connection.start();
    final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    // Create the request Queue
    final Destination requestQueue = session.createQueue(REQUEST_QUEUE_NAME);
    final MessageConsumer consumer = session.createConsumer(requestQueue);
    consumer.setMessageListener(mdbInvoker);
    // create in invoker
    final TestInterface testInterface = MdbProxy.newProxyInstance(TestInterface.class, connectionFactory, REQUEST_QUEUE_NAME);
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) Session(javax.jms.Session)

Example 72 with MessageConsumer

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

the class MdbConnectionFactoryTests method test02_directRpc.

public void test02_directRpc() throws Exception {
    final Connection connection = createConnection();
    Session session = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        // create request
        final Map<String, Object> request = new TreeMap<String, Object>();
        request.put("method", "businessMethod(java.lang.String)");
        request.put("args", new Object[] { "cheese" });
        // initialize session
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final Destination requestQueue = session.createQueue("BasicMdb");
        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);
        // System.out.println("\n" + "***************************************\n" +
        // "Sent request message: " + requestMessage + "\n" +
        // "         request map: " + request + "\n" +
        // "            to queue: " + requestQueue + "\n" +
        // "***************************************\n\n");
        // create consumer
        consumer = session.createConsumer(responseQueue);
        // System.out.println("\n" + "***************************************\n" +
        // "Listening for response at : " + responseQueue + "\n" +
        // "***************************************\n\n");
        // wait for response mesage
        final Message message = consumer.receive(1000);
        // 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 response = (Map) object;
        // process results
        if (response.containsKey("exception")) {
            throw (Exception) response.get("return");
        }
        final String returnValue = (String) response.get("return");
        assertEquals("eseehc", returnValue);
    } finally {
        MdbUtil.close(producer);
        MdbUtil.close(session);
        MdbUtil.close(connection);
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Serializable(java.io.Serializable) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) TreeMap(java.util.TreeMap) ObjectMessage(javax.jms.ObjectMessage) MessageProducer(javax.jms.MessageProducer) TreeMap(java.util.TreeMap) Map(java.util.Map) Session(javax.jms.Session)

Example 73 with MessageConsumer

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

the class JMSContextImpl method createDurableConsumer.

@Override
public JMSConsumer createDurableConsumer(final Topic topic, final String name, final String messageSelector, final boolean noLocal) {
    try {
        // JMS 2 only: final JMSConsumerImpl consumer = new JMSConsumerImpl(this, session().createDurableConsumer(topic, name, messageSelector, noLocal));
        final MessageConsumer delegate = session().createDurableSubscriber(topic, name, messageSelector, noLocal);
        checkAutoStart();
        return new JMSConsumerImpl(this, delegate);
    } catch (final JMSException e) {
        throw toRuntimeException(e);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) JMSException(javax.jms.JMSException)

Example 74 with MessageConsumer

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

the class JmsBindingTest method testHelloService.

/**
     * Send a message to HelloRequestQueue and receive from HelloResponseQueue.
     */
@Test
public void testHelloService() throws Exception {
    Session session = _hqMixIn.getJMSSession();
    MessageProducer producer = session.createProducer(HornetQMixIn.getJMSQueue(REQUEST_NAME));
    Message message = _hqMixIn.createJMSMessage(createPayload(NAME));
    producer.send(message);
    MessageConsumer consumer = session.createConsumer(HornetQMixIn.getJMSQueue(REPLY_NAME));
    message = consumer.receive(3000);
    String reply = _hqMixIn.readStringFromJMSMessage(message);
    SwitchYardTestKit.compareXMLToString(reply, createExpectedReply(NAME));
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 75 with MessageConsumer

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

the class JmsBindingTest method testRollbackA.

/**
     * Triggers the 'WorkService' by sending a HornetQ Message to the 'policyQSTransacted' queue.
     */
@Test
public void testRollbackA() throws Exception {
    String command = "rollback.A";
    Connection conn = _connectionFactory.createConnection(HORNETQ_USER, HORNETQ_PASSWORD);
    conn.start();
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(_queueIn);
        TextMessage message = session.createTextMessage();
        message.setText(command);
        producer.send(message);
        session.close();
        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(_queueOutA);
        TextMessage msg = TextMessage.class.cast(consumer.receive(30000));
        Assert.assertEquals(command, msg.getText());
        Assert.assertNull(consumer.receive(1000));
        consumer.close();
        consumer = session.createConsumer(_queueOutB);
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        Assert.assertNull(consumer.receive(1000));
        consumer.close();
        consumer = session.createConsumer(_queueOutC);
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        Assert.assertNull(consumer.receive(1000));
        session.close();
    } finally {
        conn.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

MessageConsumer (javax.jms.MessageConsumer)107 Session (javax.jms.Session)64 Message (javax.jms.Message)50 MessageProducer (javax.jms.MessageProducer)49 Connection (javax.jms.Connection)47 TextMessage (javax.jms.TextMessage)45 Test (org.junit.Test)45 Destination (javax.jms.Destination)24 JMSException (javax.jms.JMSException)23 ConnectionFactory (javax.jms.ConnectionFactory)15 TemporaryQueue (javax.jms.TemporaryQueue)9 Queue (javax.jms.Queue)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 ObjectMessage (javax.jms.ObjectMessage)7 MessageListener (javax.jms.MessageListener)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 Serializable (java.io.Serializable)3 ArrayList (java.util.ArrayList)3