Search in sources :

Example 16 with ObjectMessage

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

the class GetCallerPrincipalTestCase method testMDBLifecycle.

/**
     * Run this one in the container so it can lookup the queue
     * @throws Exception
     */
@OperateOnDeployment("test")
@Test
public void testMDBLifecycle() throws Exception {
    deployer.deploy("mdb");
    SecurityClient client = this.login();
    ITestResultsSingleton results = this.getResultsSingleton();
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    QueueConnection conn = null;
    Session session = null;
    try {
        QueueConnectionFactory qcf = (QueueConnectionFactory) new InitialContext().lookup("java:/ConnectionFactory");
        Queue queue = (Queue) new InitialContext().lookup("java:jboss/" + QUEUE_NAME);
        conn = qcf.createQueueConnection("guest", "guest");
        conn.start();
        session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        TemporaryQueue replyQueue = session.createTemporaryQueue();
        TextMessage msg = session.createTextMessage("Hello world");
        msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
        msg.setJMSReplyTo(replyQueue);
        producer = session.createProducer(queue);
        producer.send(msg);
        consumer = session.createConsumer(replyQueue);
        Message replyMsg = consumer.receive(5000);
        Object obj = ((ObjectMessage) replyMsg).getObject();
        log.trace("MDB message get: " + obj);
        Assert.assertEquals(OK + "start", results.getMdb("postconstruct"));
        deployer.undeploy("mdb");
        Assert.assertEquals(OK + "stop", results.getMdb("predestroy"));
    } finally {
        if (consumer != null) {
            consumer.close();
        }
        if (producer != null) {
            producer.close();
        }
        if (session != null) {
            session.close();
        }
        if (conn != null) {
            conn.close();
        }
        client.logout();
    }
}
Also used : SecurityClient(org.jboss.security.client.SecurityClient) MessageConsumer(javax.jms.MessageConsumer) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueConnectionFactory(javax.jms.QueueConnectionFactory) InitialContext(javax.naming.InitialContext) QueueConnection(javax.jms.QueueConnection) ObjectMessage(javax.jms.ObjectMessage) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) QueueSession(javax.jms.QueueSession) OperateOnDeployment(org.jboss.arquillian.container.test.api.OperateOnDeployment) Test(org.junit.Test)

Example 17 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 18 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<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 19 with ObjectMessage

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

Example 20 with ObjectMessage

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

the class MdbInvoker method onMessage.

@Override
public void onMessage(final Message message) {
    try {
        if (!(message instanceof ObjectMessage)) {
            return;
        }
        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 Serializable object = ((ObjectMessage) message).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");
        if (signature == null)
            throw new NullPointerException("method property is null");
        final Method method = signatures.get(signature);
        if (method == null)
            throw new IllegalArgumentException("no such method " + signature + "; known methods are " + signatures.keySet());
        final Object[] args = (Object[]) request.get("args");
        boolean exception = false;
        Object result;
        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;
        } catch (final Exception e) {
            result = e.getCause();
            if (result == null)
                result = e;
            exception = true;
        }
        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(message.getJMSCorrelationID());
            resMessage.setObject((Serializable) response);
            // send response message
            replyProducer.send(message.getJMSReplyTo(), resMessage);
        } catch (final Exception e) {
            e.printStackTrace();
        }
    } catch (final Throwable e) {
        e.printStackTrace();
    } finally {
        this.destroy();
    }
}
Also used : Serializable(java.io.Serializable) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) InvocationTargetException(java.lang.reflect.InvocationTargetException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectMessage(javax.jms.ObjectMessage) TreeMap(java.util.TreeMap) Map(java.util.Map) Session(javax.jms.Session)

Aggregations

ObjectMessage (javax.jms.ObjectMessage)47 Message (javax.jms.Message)20 Test (org.junit.Test)18 JMSException (javax.jms.JMSException)16 Session (javax.jms.Session)15 TextMessage (javax.jms.TextMessage)11 Serializable (java.io.Serializable)10 Destination (javax.jms.Destination)9 Map (java.util.Map)8 BytesMessage (javax.jms.BytesMessage)8 MapMessage (javax.jms.MapMessage)8 MessageProducer (javax.jms.MessageProducer)7 TreeMap (java.util.TreeMap)6 MessageConsumer (javax.jms.MessageConsumer)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Event (models.ChatRoom.Event)4 Connection (javax.jms.Connection)3 QueueConnection (javax.jms.QueueConnection)3 QueueSession (javax.jms.QueueSession)3 StreamMessage (javax.jms.StreamMessage)3