use of javax.jms.MessageProducer 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.MessageProducer in project ignite by apache.
the class IgniteJmsStreamerTest method produceObjectMessages.
private void produceObjectMessages(Destination dest, boolean singleMsg) throws JMSException {
Session ses = connFactory.createConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer mp = ses.createProducer(dest);
HashSet<TestTransformers.TestObject> set = new HashSet<>();
for (String key : TEST_DATA.keySet()) {
TestTransformers.TestObject to = new TestTransformers.TestObject(key, TEST_DATA.get(key));
set.add(to);
}
int messagesSent;
if (singleMsg) {
mp.send(ses.createObjectMessage(set));
messagesSent = 1;
} else {
for (TestTransformers.TestObject to : set) mp.send(ses.createObjectMessage(to));
messagesSent = set.size();
}
if (dest instanceof Queue) {
try {
assertEquals(messagesSent, broker.getBroker().getDestinationMap().get(dest).getDestinationStatistics().getMessages().getCount());
} catch (Exception e) {
fail(e.toString());
}
}
}
use of javax.jms.MessageProducer in project ignite by apache.
the class IgniteJmsStreamerTest method produceStringMessages.
private void produceStringMessages(Destination dest, boolean singleMsg) throws JMSException {
Session ses = connFactory.createConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer mp = ses.createProducer(dest);
HashSet<String> set = new HashSet<>();
for (String key : TEST_DATA.keySet()) set.add(key + "," + TEST_DATA.get(key));
int messagesSent;
if (singleMsg) {
StringBuilder sb = new StringBuilder();
for (String s : set) sb.append(s).append("|");
sb.deleteCharAt(sb.length() - 1);
mp.send(ses.createTextMessage(sb.toString()));
messagesSent = 1;
} else {
for (String s : set) mp.send(ses.createTextMessage(s));
messagesSent = set.size();
}
if (dest instanceof Queue) {
try {
assertEquals(messagesSent, broker.getBroker().getDestinationMap().get(dest).getDestinationStatistics().getMessages().getCount());
} catch (Exception e) {
fail(e.toString());
}
}
}
use of javax.jms.MessageProducer 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();
}
}
use of javax.jms.MessageProducer in project rocketmq-externals by apache.
the class JmsConsumeIT method testConsume.
@Test
public void testConsume() throws Exception {
final Topic topic = (Topic) consumeContext.getBean("baseTopic");
JmsTestListener messageListener = (JmsTestListener) consumeContext.getBean("messageListener");
CountDownLatch countDownLatch = new CountDownLatch(1);
messageListener.setLatch(countDownLatch);
messageListener.setExpectd(30);
consumeContext.start();
for (int i = 0; i < 30; i++) {
jmsTemplate.execute(topic, new ProducerCallback() {
@Override
public Object doInJms(Session session, MessageProducer producer) throws JMSException {
JmsTextMessage message = (JmsTextMessage) session.createTextMessage("hello world,kafka, haha");
producer.send(topic, message);
Assert.assertNotNull(message.getJMSMessageID());
return message;
}
});
}
if (countDownLatch.await(30, TimeUnit.SECONDS)) {
Thread.sleep(2000);
}
Assert.assertEquals(30, messageListener.getConsumedNum());
consumeContext.close();
}
Aggregations