Search in sources :

Example 26 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 27 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 28 with ObjectMessage

use of javax.jms.ObjectMessage in project vcell by virtualcell.

the class VCMessageJms method show.

public String show() {
    StringBuffer buffer = new StringBuffer();
    try {
        java.util.Enumeration enum1 = jmsMessage.getPropertyNames();
        while (enum1.hasMoreElements()) {
            String propName = (String) enum1.nextElement();
            try {
                String value = jmsMessage.getStringProperty(propName);
                buffer.append(" " + propName + "='" + value + "'");
            } catch (MessagePropertyNotFoundException ex) {
                // definitely should not happen
                delegate.onException(ex);
            }
        }
        int maxContentLength = 120;
        if (jmsMessage instanceof TextMessage) {
            buffer.append("  textContent='");
            String textContent = ((TextMessage) jmsMessage).getText();
            if (textContent != null && textContent.length() > maxContentLength) {
                buffer.append(textContent.substring(0, maxContentLength - 3) + "...");
            } else {
                buffer.append(textContent);
            }
            buffer.append("'");
        } else if (jmsMessage instanceof ObjectMessage) {
            buffer.append("  objectContent='");
            String text = "" + ((ObjectMessage) jmsMessage).getObject();
            if (text.length() > maxContentLength) {
                buffer.append(text.substring(0, maxContentLength - 3) + "...");
            } else {
                buffer.append(text);
            }
            buffer.append("'");
        }
    } catch (JMSException e) {
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage(), e);
    }
    return buffer.toString();
}
Also used : MessagePropertyNotFoundException(cbit.vcell.message.MessagePropertyNotFoundException) Enumeration(java.util.Enumeration) ObjectMessage(javax.jms.ObjectMessage) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage)

Example 29 with ObjectMessage

use of javax.jms.ObjectMessage in project ArachneCentralAPI by OHDSI.

the class BaseDataNodeMessagingController method saveCommonEntity.

private void saveCommonEntity(Principal principal, String id, Serializable object) throws PermissionDeniedException {
    DataNode dataNode = getDatanode(principal);
    String queueBase = MessagingUtils.Entities.getBaseQueue(dataNode);
    String responseQueue = getResponseQueueName(queueBase);
    jmsTemplate.send(responseQueue, session -> {
        ObjectMessage message = session.createObjectMessage(object);
        message.setJMSCorrelationID(id);
        return message;
    });
}
Also used : DataNode(com.odysseusinc.arachne.portal.model.DataNode) ObjectMessage(javax.jms.ObjectMessage)

Example 30 with ObjectMessage

use of javax.jms.ObjectMessage in project ArachneCentralAPI by OHDSI.

the class BaseDataNodeMessagingController method saveListResponse.

/**
 * Posts responses for for CommonEntity list requests
 * (for pushing by Node's back)
 */
@RequestMapping(value = "/api/v1/data-nodes/entity-lists/responses", method = POST)
public void saveListResponse(@RequestBody @Valid CommonListEntityResponseDTO commonListEntityResponseDTO, Principal principal) throws PermissionDeniedException {
    DataNode dataNode = getDatanode(principal);
    String responseQueue = getResponseQueueName(MessagingUtils.EntitiesList.getBaseQueue(dataNode));
    List<CommonEntityDTO> response = (List<CommonEntityDTO>) commonListEntityResponseDTO.getEntities();
    for (String correlationId : commonListEntityResponseDTO.getRequestIds()) {
        jmsTemplate.send(responseQueue, session -> {
            ObjectMessage message = session.createObjectMessage((Serializable) response);
            message.setJMSCorrelationID(correlationId);
            return message;
        });
    }
}
Also used : DataNode(com.odysseusinc.arachne.portal.model.DataNode) ObjectMessage(javax.jms.ObjectMessage) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CommonEntityDTO(com.odysseusinc.arachne.commons.api.v1.dto.CommonEntityDTO) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ObjectMessage (javax.jms.ObjectMessage)71 JMSException (javax.jms.JMSException)29 Message (javax.jms.Message)24 Session (javax.jms.Session)18 Test (org.junit.Test)18 TextMessage (javax.jms.TextMessage)14 Serializable (java.io.Serializable)11 Destination (javax.jms.Destination)11 Map (java.util.Map)10 BytesMessage (javax.jms.BytesMessage)10 MapMessage (javax.jms.MapMessage)9 MessageProducer (javax.jms.MessageProducer)8 MessageConsumer (javax.jms.MessageConsumer)7 TreeMap (java.util.TreeMap)6 MessageCreator (org.springframework.jms.core.MessageCreator)5 DataNode (com.odysseusinc.arachne.portal.model.DataNode)4 HashMap (java.util.HashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 RequestMessage (com.alliander.osgp.shared.infra.jms.RequestMessage)3 ConsumerTemplate (com.odysseusinc.arachne.commons.service.messaging.ConsumerTemplate)3