Search in sources :

Example 1 with ProducerCallback

use of org.springframework.jms.core.ProducerCallback 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)

Example 2 with ProducerCallback

use of org.springframework.jms.core.ProducerCallback 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)

Aggregations

JMSException (javax.jms.JMSException)2 MessageProducer (javax.jms.MessageProducer)2 Session (javax.jms.Session)2 JmsTextMessage (org.apache.rocketmq.jms.domain.message.JmsTextMessage)2 ProducerCallback (org.springframework.jms.core.ProducerCallback)2 Test (org.testng.annotations.Test)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 Topic (javax.jms.Topic)1 JmsTestListener (org.apache.rocketmq.jms.JmsTestListener)1 JmsBytesMessage (org.apache.rocketmq.jms.domain.message.JmsBytesMessage)1 JmsObjectMessage (org.apache.rocketmq.jms.domain.message.JmsObjectMessage)1