Search in sources :

Example 91 with TextMessage

use of javax.jms.TextMessage in project xmall by Exrick.

the class MyMessageListener method onMessage.

@Override
public void onMessage(Message message) {
    // 取消息内容
    TextMessage textMessage = (TextMessage) message;
    try {
        String text = textMessage.getText();
        log.info(text);
    } catch (JMSException e) {
        e.printStackTrace();
        throw new XmallException("监听消息失败");
    }
}
Also used : JMSException(javax.jms.JMSException) XmallException(cn.exrick.common.exception.XmallException) TextMessage(javax.jms.TextMessage)

Example 92 with TextMessage

use of javax.jms.TextMessage in project tech by ffyyhh995511.

the class ActiveMqService method sendTopic.

/**
 * 发送订阅
 * @param topicStr
 * @param msg
 * @return
 */
public Boolean sendTopic(String topicStr, String msg) {
    boolean status = false;
    Connection connection = null;
    Session session = null;
    MessageProducer producer = null;
    try {
        if (connectionFactory == null) {
            init();
        }
        // 从构造工厂中得到连接对象
        connection = connectionFactory.createConnection();
        // 启动
        connection.start();
        // 获取连接操作
        session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(topicStr);
        producer = session.createProducer(topic);
        // 设置不持久化
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        TextMessage message = session.createTextMessage();
        message.setText(msg);
        producer.send(message);
    } catch (Exception e) {
        logger.error(e);
    }
    return status;
}
Also used : Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 93 with TextMessage

use of javax.jms.TextMessage in project tech by ffyyhh995511.

the class ActiveMqService method receiveTopic.

/**
 * 接受active mq的topic(订阅)
 * 通过一个消费者的监听接受推送的消息
 * @param topicStr
 * @throws Exception
 */
public void receiveTopic(String topicStr) throws Exception {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
    Connection connection = connectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(topicStr);
    MessageConsumer consumer = session.createConsumer(topic);
    consumer.setMessageListener(new MessageListener() {

        public void onMessage(Message message) {
            TextMessage tm = (TextMessage) message;
            System.out.println(tm);
            try {
                System.out.println(tm.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 94 with TextMessage

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

the class MessageConverter method getContentFromJms.

public static byte[] getContentFromJms(javax.jms.Message jmsMessage) throws Exception {
    byte[] content;
    if (jmsMessage instanceof TextMessage) {
        if (StringUtils.isEmpty(((TextMessage) jmsMessage).getText())) {
            throw new IllegalArgumentException("Message body length is zero");
        }
        content = MsgConvertUtil.string2Bytes(((TextMessage) jmsMessage).getText(), Charsets.UTF_8.toString());
    } else if (jmsMessage instanceof ObjectMessage) {
        if (((ObjectMessage) jmsMessage).getObject() == null) {
            throw new IllegalArgumentException("Message body length is zero");
        }
        content = MsgConvertUtil.objectSerialize(((ObjectMessage) jmsMessage).getObject());
    } else if (jmsMessage instanceof BytesMessage) {
        JmsBytesMessage bytesMessage = (JmsBytesMessage) jmsMessage;
        if (bytesMessage.getBodyLength() == 0) {
            throw new IllegalArgumentException("Message body length is zero");
        }
        content = bytesMessage.getData();
    } else {
        throw new IllegalArgumentException("Unknown message type " + jmsMessage.getJMSType());
    }
    return content;
}
Also used : JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) JmsObjectMessage(org.apache.rocketmq.jms.domain.message.JmsObjectMessage) ObjectMessage(javax.jms.ObjectMessage) JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) BytesMessage(javax.jms.BytesMessage) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) TextMessage(javax.jms.TextMessage)

Example 95 with TextMessage

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

the class JmsClientIT method testProducerAndConsume_TwoConsumer.

@Test
public void testProducerAndConsume_TwoConsumer() throws Exception {
    Connection connection = createConnection(producerId, consumerId);
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destinationA = session.createTopic("TopicA");
    Destination destinationB = session.createTopic("TopicB");
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    JmsTestListener listenerA = new JmsTestListener(10, countDownLatch);
    JmsTestListener listenerB = new JmsTestListener(10, countDownLatch);
    try {
        // two consumers
        MessageConsumer messageConsumerA = session.createConsumer(destinationA);
        messageConsumerA.setMessageListener(listenerA);
        MessageConsumer messageConsumerB = session.createConsumer(destinationB);
        messageConsumerB.setMessageListener(listenerB);
        // producer
        MessageProducer messageProducer = session.createProducer(destinationA);
        connection.start();
        for (int i = 0; i < 10; i++) {
            TextMessage message = session.createTextMessage(text + i);
            Assert.assertNull(message.getJMSMessageID());
            messageProducer.send(message);
            Assert.assertNotNull(message.getJMSMessageID());
        }
        for (int i = 0; i < 10; i++) {
            TextMessage message = session.createTextMessage(text + i);
            Assert.assertNull(message.getJMSMessageID());
            messageProducer.send(destinationB, message);
            Assert.assertNotNull(message.getJMSMessageID());
        }
        if (countDownLatch.await(30, TimeUnit.SECONDS)) {
            Thread.sleep(2000);
        }
        Assert.assertEquals(10, listenerA.getConsumedNum());
        Assert.assertEquals(10, listenerB.getConsumedNum());
    } finally {
        // Close the connection
        connection.close();
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) JmsTestListener(org.apache.rocketmq.jms.JmsTestListener) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

TextMessage (javax.jms.TextMessage)231 Test (org.junit.Test)92 Session (javax.jms.Session)75 MessageProducer (javax.jms.MessageProducer)71 Message (javax.jms.Message)70 JMSException (javax.jms.JMSException)64 Connection (javax.jms.Connection)44 Destination (javax.jms.Destination)44 MessageConsumer (javax.jms.MessageConsumer)44 ObjectMessage (javax.jms.ObjectMessage)25 BytesMessage (javax.jms.BytesMessage)22 Queue (javax.jms.Queue)21 QueueSession (javax.jms.QueueSession)20 StubTextMessage (org.springframework.jms.StubTextMessage)18 ConnectionFactory (javax.jms.ConnectionFactory)13 QueueConnection (javax.jms.QueueConnection)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 MapMessage (javax.jms.MapMessage)11 Topic (javax.jms.Topic)11 JMSContext (javax.jms.JMSContext)9