Search in sources :

Example 36 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 37 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 38 with Message

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

the class ArtemisDestinationSourceFactory method getNames.

private List<String> getNames(Connection connection, DestinationSource.DestinationType type) {
    try {
        QueueSession session = ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue managementQueue = session.createQueue("activemq.management");
        QueueRequestor requestor = new QueueRequestor(session, managementQueue);
        connection.start();
        TextMessage m = session.createTextMessage();
        m.setStringProperty("_AMQ_ResourceName", "broker");
        m.setStringProperty("_AMQ_OperationName", "getQueueNames");
        String routing = type == DestinationSource.DestinationType.Queue ? "ANYCAST" : "MULTICAST";
        m.setText("[\"" + routing + "\"]");
        Message reply = requestor.request(m);
        String json = ((TextMessage) reply).getText();
        List<?> array = (List<?>) JsonReader.read(new StringReader(json));
        return (List<String>) array.get(0);
    } catch (Exception e) {
        return Collections.emptyList();
    }
}
Also used : QueueRequestor(javax.jms.QueueRequestor) QueueConnection(javax.jms.QueueConnection) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) StringReader(java.io.StringReader) List(java.util.List) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) JMSException(javax.jms.JMSException)

Example 39 with Message

use of javax.jms.Message in project logging-log4j2 by apache.

the class JmsAppender method append.

@Override
public void append(final LogEvent event) {
    try {
        final Message message = this.manager.createMessage(getLayout().toSerializable(event));
        message.setJMSTimestamp(event.getTimeMillis());
        this.producer.send(message);
    } catch (final JMSException e) {
        throw new AppenderLoggingException(e);
    }
}
Also used : Message(javax.jms.Message) AppenderLoggingException(org.apache.logging.log4j.core.appender.AppenderLoggingException) JMSException(javax.jms.JMSException)

Example 40 with Message

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

the class JMSSampler method sample.

/**
     * {@inheritDoc}
     */
@Override
public SampleResult sample(Entry entry) {
    SampleResult res = new SampleResult();
    res.setSampleLabel(getName());
    res.setSamplerData(getContent());
    // Assume failure
    res.setSuccessful(false);
    res.setDataType(SampleResult.TEXT);
    res.sampleStart();
    try {
        TextMessage msg = createMessage();
        if (isOneway()) {
            int deliveryMode = isNonPersistent() ? DeliveryMode.NON_PERSISTENT : DeliveryMode.PERSISTENT;
            producer.send(msg, deliveryMode, Integer.parseInt(getPriority()), Long.parseLong(getExpiration()));
            res.setRequestHeaders(Utils.messageProperties(msg));
            res.setResponseOK();
            res.setResponseData("Oneway request has no response data", null);
        } else {
            if (!useTemporyQueue()) {
                msg.setJMSReplyTo(receiveQueue);
            }
            Message replyMsg = executor.sendAndReceive(msg, isNonPersistent() ? DeliveryMode.NON_PERSISTENT : DeliveryMode.PERSISTENT, Integer.parseInt(getPriority()), Long.parseLong(getExpiration()));
            res.setRequestHeaders(Utils.messageProperties(msg));
            if (replyMsg == null) {
                res.setResponseMessage("No reply message received");
            } else {
                if (replyMsg instanceof TextMessage) {
                    res.setResponseData(((TextMessage) replyMsg).getText(), null);
                } else {
                    res.setResponseData(replyMsg.toString(), null);
                }
                res.setResponseHeaders(Utils.messageProperties(replyMsg));
                res.setResponseOK();
            }
        }
    } catch (Exception e) {
        LOGGER.warn(e.getLocalizedMessage(), e);
        if (thrown != null) {
            res.setResponseMessage(thrown.toString());
        } else {
            res.setResponseMessage(e.getLocalizedMessage());
        }
    }
    res.sampleEnd();
    return res;
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) SampleResult(org.apache.jmeter.samplers.SampleResult) TextMessage(javax.jms.TextMessage) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException)

Aggregations

Message (javax.jms.Message)230 TextMessage (javax.jms.TextMessage)103 Test (org.junit.Test)91 Session (javax.jms.Session)86 JMSException (javax.jms.JMSException)77 MessageProducer (javax.jms.MessageProducer)64 ObjectMessage (javax.jms.ObjectMessage)52 MessageConsumer (javax.jms.MessageConsumer)50 Connection (javax.jms.Connection)45 BytesMessage (javax.jms.BytesMessage)37 Destination (javax.jms.Destination)30 MapMessage (javax.jms.MapMessage)26 Queue (javax.jms.Queue)20 ConnectionFactory (javax.jms.ConnectionFactory)18 QueueSession (javax.jms.QueueSession)17 MessageCreator (org.springframework.jms.core.MessageCreator)15 MessageListener (javax.jms.MessageListener)12 Map (java.util.Map)10 StreamMessage (javax.jms.StreamMessage)10 TemporaryQueue (javax.jms.TemporaryQueue)9