Search in sources :

Example 76 with ObjectMessage

use of javax.jms.ObjectMessage in project wildfly by wildfly.

the class RunAsMDBUnitTestCase method testSendMessage.

@Test
public void testSendMessage() throws Exception {
    ConnectionFactory connFactory = lookup("ConnectionFactory", ConnectionFactory.class);
    Connection conn = connFactory.createConnection();
    conn.start();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue replyQueue = session.createTemporaryQueue();
    TextMessage msg = session.createTextMessage("Hello world");
    msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
    msg.setJMSReplyTo(replyQueue);
    Queue queue = lookup("java:jboss/" + queueName, Queue.class);
    MessageProducer producer = session.createProducer(queue);
    producer.send(msg);
    MessageConsumer consumer = session.createConsumer(replyQueue);
    Message replyMsg = consumer.receive(5000);
    Assert.assertNotNull(replyMsg);
    if (replyMsg instanceof ObjectMessage) {
        Exception e = (Exception) ((ObjectMessage) replyMsg).getObject();
        throw e;
    }
    Assert.assertTrue(replyMsg instanceof TextMessage);
    String actual = ((TextMessage) replyMsg).getText();
    Assert.assertEquals("SUCCESS", actual);
    consumer.close();
    producer.close();
    session.close();
    conn.close();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 77 with ObjectMessage

use of javax.jms.ObjectMessage in project wildfly by wildfly.

the class ObjectMessageTestCase method testObjectMessageWithObjectArray.

/**
 * Test that the MDB can process a {@link ObjectMessage} which consists of an array of objects,
 * without any classloading issues
 *
 * @throws Exception
 */
@Test
public void testObjectMessageWithObjectArray() throws Exception {
    final String goodMorning = "Good morning";
    final String goodEvening = "Good evening";
    // create the message
    final SimpleMessageInEarLibJar[] multipleGreetings = new SimpleMessageInEarLibJar[2];
    final SimpleMessageInEarLibJar messageOne = new SimpleMessageInEarLibJar(goodMorning);
    final SimpleMessageInEarLibJar messageTwo = new SimpleMessageInEarLibJar(goodEvening);
    multipleGreetings[0] = messageOne;
    multipleGreetings[1] = messageTwo;
    // send as ObjectMessage
    this.jmsUtil.sendObjectMessage(multipleGreetings, this.objectMessageOfArrayTypeQueue, this.objectMessageOfArrayTypeReplyQueue);
    // wait for an reply
    final Message reply = this.jmsUtil.receiveMessage(objectMessageOfArrayTypeReplyQueue, 5000);
    // test the reply
    Assert.assertNotNull("Reply message was null on reply queue: " + this.objectMessageOfArrayTypeReplyQueue, reply);
    final SimpleMessageInEarLibJar[] replyMessage = (SimpleMessageInEarLibJar[]) ((ObjectMessage) reply).getObject();
    Assert.assertTrue("Unexpected reply message on reply queue: " + this.objectMessageOfArrayTypeReplyQueue, Arrays.equals(replyMessage, multipleGreetings));
}
Also used : ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) MDBAcceptingObjectMessage(org.jboss.as.test.integration.ejb.mdb.objectmessage.MDBAcceptingObjectMessage) SimpleMessageInEarLibJar(org.jboss.as.test.integration.ejb.mdb.objectmessage.SimpleMessageInEarLibJar) Test(org.junit.Test)

Example 78 with ObjectMessage

use of javax.jms.ObjectMessage in project wildfly by wildfly.

the class ObjectMessageTestCase method testObjectMessage.

/**
 * Test that the MDB can process a {@link ObjectMessage} without any classloading issues
 *
 * @throws Exception
 */
@Test
public void testObjectMessage() throws Exception {
    final String goodAfternoon = "Good afternoon!";
    // create the message
    final SimpleMessageInEarLibJar message = new SimpleMessageInEarLibJar(goodAfternoon);
    // send as ObjectMessage
    this.jmsUtil.sendObjectMessage(message, this.objectMessageQueue, this.objectMessageReplyQueue);
    // wait for an reply
    final Message reply = this.jmsUtil.receiveMessage(objectMessageReplyQueue, 5000);
    // test the reply
    Assert.assertNotNull("Reply message was null on reply queue: " + this.objectMessageReplyQueue, reply);
    final SimpleMessageInEarLibJar replyMessage = (SimpleMessageInEarLibJar) ((ObjectMessage) reply).getObject();
    Assert.assertEquals("Unexpected reply message on reply queue: " + this.objectMessageReplyQueue, message, replyMessage);
}
Also used : ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) MDBAcceptingObjectMessage(org.jboss.as.test.integration.ejb.mdb.objectmessage.MDBAcceptingObjectMessage) SimpleMessageInEarLibJar(org.jboss.as.test.integration.ejb.mdb.objectmessage.SimpleMessageInEarLibJar) Test(org.junit.Test)

Example 79 with ObjectMessage

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

the class JMSProducerImpl method send.

@Override
public JMSProducer send(final Destination destination, final Serializable body) {
    final ObjectMessage message = wrap(context.createObjectMessage(body));
    send(destination, message);
    return this;
}
Also used : ObjectMessage(javax.jms.ObjectMessage)

Example 80 with ObjectMessage

use of javax.jms.ObjectMessage 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<>();
        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)

Aggregations

ObjectMessage (javax.jms.ObjectMessage)227 Test (org.junit.Test)85 JMSException (javax.jms.JMSException)82 Session (javax.jms.Session)79 Message (javax.jms.Message)70 MessageProducer (javax.jms.MessageProducer)64 MessageConsumer (javax.jms.MessageConsumer)52 Connection (javax.jms.Connection)46 Queue (javax.jms.Queue)40 TextMessage (javax.jms.TextMessage)38 BytesMessage (javax.jms.BytesMessage)30 Destination (javax.jms.Destination)29 MapMessage (javax.jms.MapMessage)28 AbstractServiceComponentAgentFunctionalIntegrationTest (com.hack23.cia.service.component.agent.impl.AbstractServiceComponentAgentFunctionalIntegrationTest)25 StreamMessage (javax.jms.StreamMessage)21 HashMap (java.util.HashMap)18 Serializable (java.io.Serializable)17 Map (java.util.Map)16 ArrayList (java.util.ArrayList)13 QueueSession (javax.jms.QueueSession)11