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("监听消息失败");
}
}
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;
}
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();
}
}
});
}
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;
}
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();
}
}
Aggregations