Search in sources :

Example 66 with MessageProducer

use of javax.jms.MessageProducer in project rocketmq-externals by apache.

the class JmsProduceIT method simpleSendTest.

@Test
public void simpleSendTest() throws Exception {
    // Send text message
    jmsTemplate.execute(destination, new ProducerCallback() {

        @Override
        public Object doInJms(Session session, MessageProducer producer) throws JMSException {
            JmsTextMessage message = (JmsTextMessage) session.createTextMessage("hello world,kafka");
            producer.send(destination, message);
            Assert.assertNotNull(message.getJMSMessageID());
            return message;
        }
    });
    // Send object message
    jmsTemplate.execute(destination, new ProducerCallback() {

        @Override
        public Object doInJms(Session session, MessageProducer producer) throws JMSException {
            JmsObjectMessage message = (JmsObjectMessage) session.createObjectMessage(Lists.newArrayList(1, 2, 3));
            producer.send(destination, message);
            Assert.assertNotNull(message.getJMSMessageID());
            return message;
        }
    });
    // Send byte message
    jmsTemplate.execute(destination, new ProducerCallback() {

        @Override
        public Object doInJms(Session session, MessageProducer producer) throws JMSException {
            byte[] ts = "Von,Test".getBytes();
            JmsBytesMessage message = (JmsBytesMessage) session.createBytesMessage();
            message.writeBytes(ts);
            producer.send(destination, message);
            Assert.assertNotNull(message.getJMSMessageID());
            return message;
        }
    });
}
Also used : JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) ProducerCallback(org.springframework.jms.core.ProducerCallback) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) JmsObjectMessage(org.apache.rocketmq.jms.domain.message.JmsObjectMessage) Session(javax.jms.Session) Test(org.testng.annotations.Test)

Example 67 with MessageProducer

use of javax.jms.MessageProducer in project hive by apache.

the class NotificationListener method isConnectionHealthy.

/**
 * Send a dummy message to probe if the JMS connection is healthy
 * @return true if connection is healthy, false otherwise
 */
protected boolean isConnectionHealthy() {
    try {
        Topic topic = createTopic(getTopicPrefix(getConf()) + "." + HEALTH_CHECK_TOPIC_SUFFIX);
        MessageProducer producer = createProducer(topic);
        Message msg = session.get().createTextMessage(HEALTH_CHECK_MSG);
        producer.send(msg, DeliveryMode.NON_PERSISTENT, 4, 0);
    } catch (Exception e) {
        return false;
    }
    return true;
}
Also used : HCatEventMessage(org.apache.hive.hcatalog.messaging.HCatEventMessage) Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) NamingException(javax.naming.NamingException) TException(org.apache.thrift.TException) JMSException(javax.jms.JMSException)

Example 68 with MessageProducer

use of javax.jms.MessageProducer in project wso2-axis2-transports by wso2.

the class MockEchoEndpoint method setUp.

@Setup
@SuppressWarnings("unused")
private void setUp(JMSTestEnvironment env, JMSRequestResponseChannel channel) throws Exception {
    Destination destination = channel.getDestination();
    Destination replyDestination = channel.getReplyDestination();
    connection = env.getConnectionFactory().createConnection();
    connection.setExceptionListener(this);
    connection.start();
    replyConnection = env.getConnectionFactory().createConnection();
    replyConnection.setExceptionListener(this);
    final Session replySession = replyConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final MessageProducer producer = replySession.createProducer(replyDestination);
    MessageConsumer consumer = connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {

        public void onMessage(Message message) {
            try {
                log.info("Message received: ID = " + message.getJMSMessageID());
                Message reply;
                if (message instanceof BytesMessage) {
                    reply = replySession.createBytesMessage();
                    IOUtils.copy(new BytesMessageInputStream((BytesMessage) message), new BytesMessageOutputStream((BytesMessage) reply));
                } else if (message instanceof TextMessage) {
                    reply = replySession.createTextMessage();
                    ((TextMessage) reply).setText(((TextMessage) message).getText());
                } else {
                    // TODO
                    throw new UnsupportedOperationException("Unsupported message type");
                }
                reply.setJMSCorrelationID(message.getJMSMessageID());
                reply.setStringProperty(BaseConstants.CONTENT_TYPE, message.getStringProperty(BaseConstants.CONTENT_TYPE));
                producer.send(reply);
                log.info("Message sent: ID = " + reply.getJMSMessageID());
            } catch (Throwable ex) {
                fireEndpointError(ex);
            }
        }
    });
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) BytesMessage(javax.jms.BytesMessage) BytesMessageOutputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageOutputStream) MessageProducer(javax.jms.MessageProducer) BytesMessageInputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Setup(org.apache.axis2.transport.testkit.tests.Setup)

Example 69 with MessageProducer

use of javax.jms.MessageProducer in project hono by eclipse.

the class SendReceiveIT method testMalformedTelemetryMessageGetsRejected.

/**
 * Verifies that the Telemetry endpoint rejects a malformed message.
 *
 * @throws Exception if the test fails.
 */
@Test
public void testMalformedTelemetryMessageGetsRejected() throws Exception {
    givenAReceiver();
    givenASender();
    final CountDownLatch rejected = new CountDownLatch(1);
    // prepare consumer to get some credits for sending
    final MessageConsumer messageConsumer = receiver.getTelemetryConsumer();
    messageConsumer.setMessageListener(message -> {
    });
    final MessageProducer messageProducer = sender.getTelemetryProducer();
    // message lacks device ID and registration assertion
    final Message message = sender.newMessage("body", null, null);
    messageProducer.send(message, new CompletionListener() {

        @Override
        public void onException(final Message message, final Exception exception) {
            LOG.debug("failed to send message as expected: {}", exception.getMessage());
            rejected.countDown();
        }

        @Override
        public void onCompletion(final Message message) {
        // should not happen
        }
    });
    assertTrue(rejected.await(DEFAULT_TEST_TIMEOUT, TimeUnit.MILLISECONDS));
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) CompletionListener(javax.jms.CompletionListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 70 with MessageProducer

use of javax.jms.MessageProducer in project cxf by apache.

the class MessageListenerTest method sendMessage.

private void sendMessage(Connection connection, Destination dest, String content) throws JMSException, InterruptedException {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer prod = session.createProducer(dest);
    Message message = session.createTextMessage(content);
    prod.send(message);
    prod.close();
    session.close();
    // Give receiver some time to process
    Thread.sleep(500);
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Aggregations

MessageProducer (javax.jms.MessageProducer)184 Session (javax.jms.Session)131 TextMessage (javax.jms.TextMessage)74 Message (javax.jms.Message)64 Connection (javax.jms.Connection)60 Test (org.junit.Test)54 MessageConsumer (javax.jms.MessageConsumer)49 JMSException (javax.jms.JMSException)46 Topic (javax.jms.Topic)34 Destination (javax.jms.Destination)31 Queue (javax.jms.Queue)21 ConnectionFactory (javax.jms.ConnectionFactory)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 ObjectMessage (javax.jms.ObjectMessage)10 ModelNode (org.jboss.dmr.ModelNode)10 BytesMessage (javax.jms.BytesMessage)9 QueueSession (javax.jms.QueueSession)9 TemporaryQueue (javax.jms.TemporaryQueue)9 QueueConnection (javax.jms.QueueConnection)7 InitialContext (javax.naming.InitialContext)7