Search in sources :

Example 61 with MessageProducer

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;
}
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 62 with MessageProducer

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());
        }
    }
}
Also used : MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) JMSException(javax.jms.JMSException) Session(javax.jms.Session) HashSet(java.util.HashSet)

Example 63 with MessageProducer

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());
        }
    }
}
Also used : MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) JMSException(javax.jms.JMSException) Session(javax.jms.Session) HashSet(java.util.HashSet)

Example 64 with MessageProducer

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();
    }
}
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)

Example 65 with MessageProducer

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();
}
Also used : JmsTestListener(org.apache.rocketmq.jms.JmsTestListener) ProducerCallback(org.springframework.jms.core.ProducerCallback) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) CountDownLatch(java.util.concurrent.CountDownLatch) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) Session(javax.jms.Session) Test(org.testng.annotations.Test)

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