Search in sources :

Example 21 with ObjectMessage

use of javax.jms.ObjectMessage in project simba-os by cegeka.

the class RefreshCacheEventListenerTest method onMessage_UserAuthorizationChanged.

@Test
public void onMessage_UserAuthorizationChanged() throws Exception {
    SimbaEvent event = new SimbaEvent(SimbaEventType.USER_AUTHORIZATION_CHANGED, USERNAME);
    ObjectMessage message = mock(ObjectMessage.class);
    when(message.getObject()).thenReturn(event);
    listener.onMessage(message);
    verify(service).invalidate(USERNAME);
}
Also used : ObjectMessage(javax.jms.ObjectMessage) SimbaEvent(org.simbasecurity.common.event.SimbaEvent) Test(org.junit.Test)

Example 22 with ObjectMessage

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

the class JMSMessageListenerAdapter method runImpl.

protected boolean runImpl() {
    try {
        Message message = consumer.receive();
        if (message == null) {
            LOG.debug("Message consumer was closed.");
            return true;
        }
        ObjectMessage omessage = (ObjectMessage) message;
        daemonThreadStatsCollector.captureStats(thread.getId());
        listener.onMessage((GoMessage) omessage.getObject());
    } catch (JMSException e) {
        LOG.warn("Error receiving message. Message receiving will continue despite this error.", e);
    } catch (Exception e) {
        LOG.error("Exception thrown in message handling by listener " + listener, e);
    } finally {
        daemonThreadStatsCollector.clearStats(thread.getId());
    }
    return false;
}
Also used : ObjectMessage(javax.jms.ObjectMessage) GoMessage(com.thoughtworks.go.server.messaging.GoMessage) Message(javax.jms.Message) ObjectMessage(javax.jms.ObjectMessage) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException)

Example 23 with ObjectMessage

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

the class JmsBinding method createJmsMessageForType.

/**
     *
     * Create the {@link Message}
     *
     * @return jmsMessage or null if the mapping was not successfully
     */
protected Message createJmsMessageForType(Exchange exchange, Object body, Map<String, Object> headers, Session session, CamelContext context, JmsMessageType type) throws JMSException {
    switch(type) {
        case Text:
            {
                TextMessage message = session.createTextMessage();
                if (body != null) {
                    String payload = context.getTypeConverter().convertTo(String.class, exchange, body);
                    message.setText(payload);
                }
                return message;
            }
        case Bytes:
            {
                BytesMessage message = session.createBytesMessage();
                if (body != null) {
                    byte[] payload = context.getTypeConverter().convertTo(byte[].class, exchange, body);
                    message.writeBytes(payload);
                }
                return message;
            }
        case Map:
            {
                MapMessage message = session.createMapMessage();
                if (body != null) {
                    Map<?, ?> payload = context.getTypeConverter().convertTo(Map.class, exchange, body);
                    populateMapMessage(message, payload, context);
                }
                return message;
            }
        case Object:
            ObjectMessage message = session.createObjectMessage();
            if (body != null) {
                try {
                    Serializable payload = context.getTypeConverter().mandatoryConvertTo(Serializable.class, exchange, body);
                    message.setObject(payload);
                } catch (NoTypeConversionAvailableException e) {
                    // cannot convert to serializable then thrown an exception to avoid sending a null message
                    JMSException cause = new MessageFormatException(e.getMessage());
                    cause.initCause(e);
                    throw cause;
                }
            }
            return message;
        default:
            break;
    }
    return null;
}
Also used : MessageFormatException(javax.jms.MessageFormatException) Serializable(java.io.Serializable) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) HashMap(java.util.HashMap) Map(java.util.Map) TextMessage(javax.jms.TextMessage)

Example 24 with ObjectMessage

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

the class JmsBinding method createJmsMessageForType.

/**
     * 
     * Create the {@link Message} 
     * 
     * @return jmsMessage or null if the mapping was not successfully
     */
protected Message createJmsMessageForType(Exchange exchange, Object body, Map<String, Object> headers, Session session, CamelContext context, JmsMessageType type) throws JMSException {
    switch(type) {
        case Text:
            {
                TextMessage message = session.createTextMessage();
                if (body != null) {
                    String payload = context.getTypeConverter().convertTo(String.class, exchange, body);
                    message.setText(payload);
                }
                return message;
            }
        case Bytes:
            {
                BytesMessage message = session.createBytesMessage();
                if (body != null) {
                    byte[] payload = context.getTypeConverter().convertTo(byte[].class, exchange, body);
                    message.writeBytes(payload);
                }
                return message;
            }
        case Map:
            {
                MapMessage message = session.createMapMessage();
                if (body != null) {
                    Map<?, ?> payload = context.getTypeConverter().convertTo(Map.class, exchange, body);
                    populateMapMessage(message, payload, context);
                }
                return message;
            }
        case Object:
            ObjectMessage message = session.createObjectMessage();
            if (body != null) {
                try {
                    Serializable payload = context.getTypeConverter().mandatoryConvertTo(Serializable.class, exchange, body);
                    message.setObject(payload);
                } catch (NoTypeConversionAvailableException e) {
                    // cannot convert to serializable then thrown an exception to avoid sending a null message
                    JMSException cause = new MessageFormatException(e.getMessage());
                    cause.initCause(e);
                    throw cause;
                }
            }
            return message;
        default:
            break;
    }
    return null;
}
Also used : MessageFormatException(javax.jms.MessageFormatException) Serializable(java.io.Serializable) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) HashMap(java.util.HashMap) Map(org.apache.camel.component.jms.JmsMessageType.Map) Map(java.util.Map) TextMessage(javax.jms.TextMessage)

Example 25 with ObjectMessage

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

the class JmsBinding method makeJmsMessage.

/**
     * Creates a JMS message from the Camel exchange and message
     *
     * @param exchange the current exchange
     * @param camelMessage the body to make a javax.jms.Message as
     * @param session the JMS session used to create the message
     * @param cause optional exception occurred that should be sent as reply instead of a regular body
     * @return a newly created JMS Message instance containing the
     * @throws JMSException if the message could not be created
     */
public Message makeJmsMessage(Exchange exchange, org.apache.camel.Message camelMessage, Session session, Exception cause) throws JMSException {
    Message answer = null;
    boolean alwaysCopy = endpoint != null && endpoint.getConfiguration().isAlwaysCopyMessage();
    boolean force = endpoint != null && endpoint.getConfiguration().isForceSendOriginalMessage();
    if (!alwaysCopy && camelMessage instanceof JmsMessage) {
        JmsMessage jmsMessage = (JmsMessage) camelMessage;
        if (!jmsMessage.shouldCreateNewMessage() || force) {
            answer = jmsMessage.getJmsMessage();
            if (!force) {
                // answer must match endpoint type
                JmsMessageType type = endpoint != null ? endpoint.getConfiguration().getJmsMessageType() : null;
                if (type != null && answer != null) {
                    if (type == JmsMessageType.Text) {
                        answer = answer instanceof TextMessage ? answer : null;
                    } else if (type == JmsMessageType.Bytes) {
                        answer = answer instanceof BytesMessage ? answer : null;
                    } else if (type == JmsMessageType.Map) {
                        answer = answer instanceof MapMessage ? answer : null;
                    } else if (type == JmsMessageType.Object) {
                        answer = answer instanceof ObjectMessage ? answer : null;
                    } else if (type == JmsMessageType.Stream) {
                        answer = answer instanceof StreamMessage ? answer : null;
                    }
                }
            }
        }
    }
    if (answer == null) {
        if (cause != null) {
            // an exception occurred so send it as response
            LOG.debug("Will create JmsMessage with caused exception: {}", cause);
            // create jms message containing the caused exception
            answer = createJmsMessage(cause, session);
        } else {
            ObjectHelper.notNull(camelMessage, "message");
            // create regular jms message using the camel message body
            answer = createJmsMessage(exchange, camelMessage, session, exchange.getContext());
            appendJmsProperties(answer, exchange, camelMessage);
        }
    }
    if (answer != null && messageCreatedStrategy != null) {
        messageCreatedStrategy.onMessageCreated(answer, session, exchange, null);
    }
    return answer;
}
Also used : MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) TextMessage(javax.jms.TextMessage)

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