Search in sources :

Example 11 with MessageCreator

use of org.springframework.jms.core.MessageCreator in project camel by apache.

the class JmsJMSReplyToEndpointUsingInOutTest method testCustomJMSReplyToInOut.

@Test
public void testCustomJMSReplyToInOut() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("My name is Arnio");
    // do not use Camel to send and receive to simulate a non Camel client
    // use another thread to listen and send the reply
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(new Callable<Object>() {

        public Object call() throws Exception {
            JmsTemplate jms = new JmsTemplate(amq.getConfiguration().getConnectionFactory());
            final TextMessage msg = (TextMessage) jms.receive("nameRequestor");
            assertEquals("What's your name", msg.getText());
            // there should be a JMSReplyTo so we know where to send the reply
            final Destination replyTo = msg.getJMSReplyTo();
            // send reply
            jms.send(replyTo, new MessageCreator() {

                public Message createMessage(Session session) throws JMSException {
                    TextMessage replyMsg = session.createTextMessage();
                    replyMsg.setText("My name is Arnio");
                    replyMsg.setJMSCorrelationID(msg.getJMSCorrelationID());
                    return replyMsg;
                }
            });
            return null;
        }
    });
    // now get started and send the first message that gets the ball rolling
    JmsTemplate jms = new JmsTemplate(amq.getConfiguration().getConnectionFactory());
    jms.send("hello", new MessageCreator() {

        public Message createMessage(Session session) throws JMSException {
            TextMessage msg = session.createTextMessage();
            msg.setText("Hello, I'm here");
            return msg;
        }
    });
    assertMockEndpointsSatisfied();
    executor.shutdownNow();
}
Also used : Destination(javax.jms.Destination) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ExecutorService(java.util.concurrent.ExecutorService) JmsTemplate(org.springframework.jms.core.JmsTemplate) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage) MessageCreator(org.springframework.jms.core.MessageCreator) Session(javax.jms.Session) Test(org.junit.Test)

Example 12 with MessageCreator

use of org.springframework.jms.core.MessageCreator in project camel by apache.

the class ConsumeJmsBytesMessageTest method testConsumeBytesMessage.

@Test
public void testConsumeBytesMessage() throws Exception {
    endpoint.expectedMessageCount(1);
    jmsTemplate.setPubSubDomain(false);
    jmsTemplate.send("test.bytes", new MessageCreator() {

        public Message createMessage(Session session) throws JMSException {
            BytesMessage bytesMessage = session.createBytesMessage();
            bytesMessage.writeByte((byte) 1);
            bytesMessage.writeByte((byte) 2);
            bytesMessage.writeByte((byte) 3);
            return bytesMessage;
        }
    });
    endpoint.assertIsSatisfied();
    assertCorrectBytesReceived();
}
Also used : BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) JMSException(javax.jms.JMSException) BytesMessage(javax.jms.BytesMessage) MessageCreator(org.springframework.jms.core.MessageCreator) Session(javax.jms.Session) Test(org.junit.Test)

Example 13 with MessageCreator

use of org.springframework.jms.core.MessageCreator in project camel by apache.

the class ConsumeJmsMapMessageTest method testConsumeMapMessage.

@Test
public void testConsumeMapMessage() throws Exception {
    endpoint.expectedMessageCount(1);
    jmsTemplate.setPubSubDomain(false);
    jmsTemplate.send("test.map", new MessageCreator() {

        public Message createMessage(Session session) throws JMSException {
            MapMessage mapMessage = session.createMapMessage();
            mapMessage.setString("foo", "abc");
            mapMessage.setString("bar", "xyz");
            return mapMessage;
        }
    });
    endpoint.assertIsSatisfied();
    assertCorrectMapReceived();
}
Also used : MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) MessageCreator(org.springframework.jms.core.MessageCreator) Session(javax.jms.Session) Test(org.junit.Test)

Example 14 with MessageCreator

use of org.springframework.jms.core.MessageCreator in project spring-framework by spring-projects.

the class JmsTransactionManagerTests method testTransactionCommitWithMessageProducer.

@Test
public void testTransactionCommitWithMessageProducer() throws JMSException {
    Destination dest = new StubQueue();
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    Session session = mock(Session.class);
    MessageProducer producer = mock(MessageProducer.class);
    final Message message = mock(Message.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    given(session.createProducer(dest)).willReturn(producer);
    given(session.getTransacted()).willReturn(true);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    JmsTemplate jt = new JmsTemplate(cf);
    jt.send(dest, new MessageCreator() {

        @Override
        public Message createMessage(Session session) throws JMSException {
            return message;
        }
    });
    tm.commit(ts);
    verify(producer).send(message);
    verify(session).commit();
    verify(producer).close();
    verify(session).close();
    verify(con).close();
}
Also used : Destination(javax.jms.Destination) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(javax.jms.Message) Connection(javax.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JMSException(javax.jms.JMSException) MessageCreator(org.springframework.jms.core.MessageCreator) ConnectionFactory(javax.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) StubQueue(org.springframework.jms.StubQueue) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 15 with MessageCreator

use of org.springframework.jms.core.MessageCreator in project Protocol-Adapter-OSLP by OSGP.

the class OslpLogItemRequestMessageSender method send.

public void send(final OslpLogItemRequestMessage oslpLogItemRequestMessage) {
    LOGGER.debug("Sending OslpLogItemRequestMessage");
    this.oslpLogItemRequestsJmsTemplate.send(new MessageCreator() {

        @Override
        public Message createMessage(final Session session) throws JMSException {
            final ObjectMessage objectMessage = session.createObjectMessage();
            objectMessage.setJMSType(Constants.OSLP_LOG_ITEM_REQUEST);
            objectMessage.setStringProperty(Constants.IS_INCOMING, oslpLogItemRequestMessage.isIncoming().toString());
            objectMessage.setStringProperty(Constants.DEVICE_UID, oslpLogItemRequestMessage.getDeviceUid());
            objectMessage.setStringProperty(Constants.ENCODED_MESSAGE, oslpLogItemRequestMessage.getEncodedMessage());
            objectMessage.setStringProperty(Constants.DECODED_MESSAGE, oslpLogItemRequestMessage.getDecodedMessage());
            objectMessage.setStringProperty(Constants.DEVICE_IDENTIFICATION, oslpLogItemRequestMessage.getDeviceIdentification());
            objectMessage.setStringProperty(Constants.ORGANISATION_IDENTIFICATION, oslpLogItemRequestMessage.getOrganisationIdentification());
            objectMessage.setStringProperty(Constants.IS_VALID, oslpLogItemRequestMessage.isValid().toString());
            objectMessage.setIntProperty(Constants.PAYLOAD_MESSAGE_SERIALIZED_SIZE, oslpLogItemRequestMessage.getPayloadMessageSerializedSize());
            return objectMessage;
        }
    });
}
Also used : ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) ObjectMessage(javax.jms.ObjectMessage) JMSException(javax.jms.JMSException) MessageCreator(org.springframework.jms.core.MessageCreator) Session(javax.jms.Session)

Aggregations

Message (javax.jms.Message)15 Session (javax.jms.Session)15 MessageCreator (org.springframework.jms.core.MessageCreator)15 JMSException (javax.jms.JMSException)13 Test (org.junit.Test)7 ObjectMessage (javax.jms.ObjectMessage)6 Destination (javax.jms.Destination)4 RequestMessage (com.alliander.osgp.shared.infra.jms.RequestMessage)3 TextMessage (javax.jms.TextMessage)3 JmsTemplate (org.springframework.jms.core.JmsTemplate)3 UseMessageIdAsCorrelationIdMessageSentCallback (org.apache.camel.component.jms.reply.UseMessageIdAsCorrelationIdMessageSentCallback)2 File (java.io.File)1 ExecutorService (java.util.concurrent.ExecutorService)1 BytesMessage (javax.jms.BytesMessage)1 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 MapMessage (javax.jms.MapMessage)1 MessageProducer (javax.jms.MessageProducer)1 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)1 RuntimeExchangeException (org.apache.camel.RuntimeExchangeException)1