Search in sources :

Example 96 with Message

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

the class TransactedTopicMessageSender method sendToTopicSuccessfully.

@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void sendToTopicSuccessfully() throws Exception {
    Connection connection = null;
    Session session = null;
    try {
        logger.trace("Creating a Connection");
        connection = factory.createConnection();
        logger.trace("Creating a Session");
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(topic);
        Message message = session.createTextMessage("Hello world!");
        logger.trace("Sending message");
        producer.send(message);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) JMSException(javax.jms.JMSException) Session(javax.jms.Session) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 97 with Message

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

the class JmsClientTestCase method doSendAndReceive.

private void doSendAndReceive(String connectionFactoryLookup) throws Exception {
    Connection conn = null;
    try {
        ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup(connectionFactoryLookup);
        assertNotNull(cf);
        Destination destination = (Destination) remoteContext.lookup(QUEUE_NAME);
        assertNotNull(destination);
        conn = cf.createConnection("guest", "guest");
        conn.start();
        Session consumerSession = conn.createSession(false, AUTO_ACKNOWLEDGE);
        final CountDownLatch latch = new CountDownLatch(10);
        final List<String> result = new ArrayList<String>();
        // Set the async listener
        MessageConsumer consumer = consumerSession.createConsumer(destination);
        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                TextMessage msg = (TextMessage) message;
                try {
                    result.add(msg.getText());
                    latch.countDown();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        final Session producerSession = conn.createSession(false, AUTO_ACKNOWLEDGE);
        MessageProducer producer = producerSession.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        for (int i = 0; i < 10; i++) {
            String s = "Test" + i;
            TextMessage msg = producerSession.createTextMessage(s);
            //System.out.println("sending " + s);
            producer.send(msg);
        }
        producerSession.close();
        assertTrue(latch.await(3, SECONDS));
        assertEquals(10, result.size());
        for (int i = 0; i < result.size(); i++) {
            assertEquals("Test" + i, result.get(i));
        }
    } finally {
        try {
            conn.close();
        } catch (Exception ignore) {
        }
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) JMSException(javax.jms.JMSException) ConnectionFactory(javax.jms.ConnectionFactory) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 98 with Message

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

the class JMS2AMQTest method receiveGetBody.

@Test
public void receiveGetBody() throws InterruptedException {
    final String text = TEXT + "2";
    final AtomicReference<Throwable> error = new AtomicReference<>();
    final CountDownLatch ready = new CountDownLatch(1);
    final CountDownLatch over = new CountDownLatch(1);
    new Thread() {

        @Override
        public void run() {
            {
                setName(JMS2AMQTest.class.getName() + ".receiveGetBody#receiver");
            }
            try (final JMSContext context = cf.createContext()) {
                try (final JMSConsumer consumer = context.createConsumer(destination2)) {
                    ready.countDown();
                    final Message receive = consumer.receive(TimeUnit.MINUTES.toMillis(1));
                    assertEquals(text, receive.getBody(String.class));
                }
            } catch (final Throwable ex) {
                error.set(ex);
            } finally {
                over.countDown();
            }
        }
    }.start();
    ready.await(1, TimeUnit.MINUTES);
    // just to ensure we called receive already
    sleep(150);
    // now send the message
    try (final JMSContext context = cf.createContext()) {
        context.createProducer().send(destination2, text);
    } catch (final JMSRuntimeException ex) {
        fail(ex.getMessage());
    }
    over.await(1, TimeUnit.MINUTES);
    // ensure we got the message and no exception
    final Throwable exception = error.get();
    if (exception != null) {
        exception.printStackTrace();
    }
    assertNull(exception == null ? "ok" : exception.getMessage(), exception);
}
Also used : JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 99 with Message

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

the class JMSConsumerImpl method receiveBodyNoWait.

@Override
public <T> T receiveBodyNoWait(final Class<T> c) {
    try {
        final Message message = wrap(consumer.receiveNoWait());
        context.setLastMessage(message);
        return message == null ? null : message.getBody(c);
    } catch (JMSException e) {
        throw toRuntimeException(e);
    }
}
Also used : Message(javax.jms.Message) JMSException(javax.jms.JMSException)

Example 100 with Message

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

the class JMSConsumerImpl method receiveBody.

@Override
public <T> T receiveBody(final Class<T> c, final long timeout) {
    try {
        final Message message = wrap(consumer.receive(timeout));
        context.setLastMessage(message);
        return message == null ? null : message.getBody(c);
    } catch (final JMSException e) {
        throw toRuntimeException(e);
    }
}
Also used : Message(javax.jms.Message) JMSException(javax.jms.JMSException)

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