Search in sources :

Example 16 with Message

use of javax.jms.Message in project quickstarts by jboss-switchyard.

the class JMSClient method sendToActiveMQ.

private static void sendToActiveMQ(String value) throws Exception {
    ConnectionFactory cf = new ActiveMQConnectionFactory(AMQ_USER, AMQ_PASSWD, AMQ_BROKER_URL);
    Connection conn = cf.createConnection();
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MessageProducer producer = session.createProducer(session.createQueue(REQUEST_NAME));
        final MessageConsumer consumer = session.createConsumer(session.createQueue(REPLY_NAME));
        conn.start();
        producer.send(session.createTextMessage(createPayload(value)));
        System.out.println("Message sent. Waiting for reply ...");
        Message message = consumer.receive(3000);
        String reply = ((TextMessage) message).getText();
        System.out.println("REPLY: \n" + reply);
    } finally {
        conn.close();
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 17 with Message

use of javax.jms.Message in project quickstarts by jboss-switchyard.

the class ActiveMQClient method main.

/**
     * Only execution point for this application.
     * @param ignored not used.
     * @throws Exception if something goes wrong.
     */
public static void main(final String[] args) throws Exception {
    String[] orders = { "BREAD", "PIZZA", "JAM", "POTATO", "MILK", "JAM" };
    if (args.length != 0) {
        orders = args;
    }
    ActiveMQMixIn mixIn = new ActiveMQMixIn();
    try {
        Session session = mixIn.getSession();
        MessageProducer producer = session.createProducer(session.createQueue(ORDER_QUEUE));
        for (String order : orders) {
            final TextMessage message = session.createTextMessage();
            message.setText(order);
            producer.send(message);
        }
        session.close();
        session = mixIn.getSession();
        System.out.println("* * *  SHIPPING ORDERS  * * *");
        MessageConsumer consumer = session.createConsumer(session.createQueue(SHIPPING_QUEUE));
        Message msg = null;
        while ((msg = consumer.receive(1000)) != null) {
            if (msg instanceof TextMessage) {
                System.out.println(" - " + ((TextMessage) msg).getText());
            }
        }
        System.out.println();
        System.out.println("* * *  PENDING ORDERS (FILLING STOCK)  * * *");
        consumer = session.createConsumer(session.createQueue(FILLING_STOCK_QUEUE));
        while ((msg = consumer.receive(1000)) != null) {
            if (msg instanceof TextMessage) {
                System.out.println(" - " + ((TextMessage) msg).getText());
            }
        }
        session.close();
        Thread.sleep(2000);
    } finally {
        mixIn.uninitialize();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) ActiveMQMixIn(org.switchyard.component.test.mixins.activemq.ActiveMQMixIn) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 18 with Message

use of javax.jms.Message in project pinpoint by naver.

the class ActiveMQClientITBase method testQueuePull.

@Test
public void testQueuePull() throws Exception {
    // Given
    final String testQueueName = "TestPullQueue";
    final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName);
    final String testMessage = "Hello World for Queue!";
    // create producer
    ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
    MessageProducer producer = producerSession.createProducer(testQueue);
    final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
    // When
    ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    MessageConsumer consumer = consumerSession.createConsumer(testQueue);
    // Then
    producer.send(expectedTextMessage);
    Message message = consumer.receive(1000L);
    Assert.assertEquals(testMessage, ((TextMessage) message).getText());
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    awaitAndVerifyTraceCount(5, 5000L);
    // trace count : 1
    verifyProducerSendEvent(testQueue);
    // trace count : 4
    verifyConsumerPullEvent(testQueue, consumer, expectedTextMessage);
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) ActiveMQSession(org.apache.activemq.ActiveMQSession) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 19 with Message

use of javax.jms.Message in project spring-framework by spring-projects.

the class JmsInvokerServiceExporter method writeRemoteInvocationResult.

/**
	 * Send the given RemoteInvocationResult as a JMS message to the originator.
	 * @param requestMessage current request message
	 * @param session the JMS Session to use
	 * @param result the RemoteInvocationResult object
	 * @throws javax.jms.JMSException if thrown by trying to send the message
	 */
protected void writeRemoteInvocationResult(Message requestMessage, Session session, RemoteInvocationResult result) throws JMSException {
    Message response = createResponseMessage(requestMessage, session, result);
    MessageProducer producer = session.createProducer(requestMessage.getJMSReplyTo());
    try {
        producer.send(response);
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}
Also used : Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer)

Example 20 with Message

use of javax.jms.Message in project spring-framework by spring-projects.

the class MappingJackson2MessageConverter method toMessage.

@Override
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
    Message message;
    try {
        switch(this.targetType) {
            case TEXT:
                message = mapToTextMessage(object, session, this.objectMapper.writer());
                break;
            case BYTES:
                message = mapToBytesMessage(object, session, this.objectMapper.writer());
                break;
            default:
                message = mapToMessage(object, session, this.objectMapper.writer(), this.targetType);
        }
    } catch (IOException ex) {
        throw new MessageConversionException("Could not map JSON object [" + object + "]", ex);
    }
    setTypeIdOnMessage(object, message);
    return message;
}
Also used : TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) IOException(java.io.IOException)

Aggregations

Message (javax.jms.Message)191 TextMessage (javax.jms.TextMessage)91 Test (org.junit.Test)84 Session (javax.jms.Session)73 JMSException (javax.jms.JMSException)59 MessageProducer (javax.jms.MessageProducer)53 ObjectMessage (javax.jms.ObjectMessage)46 MessageConsumer (javax.jms.MessageConsumer)40 Connection (javax.jms.Connection)38 BytesMessage (javax.jms.BytesMessage)31 Destination (javax.jms.Destination)27 MapMessage (javax.jms.MapMessage)25 Queue (javax.jms.Queue)20 QueueSession (javax.jms.QueueSession)17 ConnectionFactory (javax.jms.ConnectionFactory)16 MessageCreator (org.springframework.jms.core.MessageCreator)12 MessageListener (javax.jms.MessageListener)9 AMQPMessage (org.apache.qpid.qmf2.common.AMQPMessage)9 Map (java.util.Map)8 QueueConnection (javax.jms.QueueConnection)8