Java Code Examples for org.springframework.jms.core.JmsTemplate#send()

The following examples show how to use org.springframework.jms.core.JmsTemplate#send() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JMSPublisherConsumerIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * At the moment the only two supported message types are TextMessage and
 * BytesMessage which is sufficient for the type if JMS use cases NiFi is
 * used. The may change to the point where all message types are supported
 * at which point this test will no be longer required.
 */
@Test
public void validateFailOnUnsupportedMessageType() throws Exception {
    final String destinationName = "validateFailOnUnsupportedMessageType";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    try {
        jmsTemplate.send(destinationName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createObjectMessage();
            }
        });

        JMSConsumer consumer = new JMSConsumer((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        consumer.consume(destinationName, null, false, false, null, "UTF-8", new ConsumerCallback() {
            @Override
            public void accept(JMSResponse response) {
                // noop
            }
        });
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 2
Source File: JMSPublisherConsumerTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * At the moment the only two supported message types are TextMessage and
 * BytesMessage which is sufficient for the type if JMS use cases NiFi is
 * used. The may change to the point where all message types are supported
 * at which point this test will no be longer required.
 */
@Test(expected = IllegalStateException.class)
public void validateFailOnUnsupportedMessageType() throws Exception {
    final String destinationName = "testQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    jmsTemplate.send(destinationName, new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createObjectMessage();
        }
    });

    JMSConsumer consumer = new JMSConsumer(jmsTemplate, mock(ComponentLog.class));
    try {
        consumer.consume(destinationName, new ConsumerCallback() {
            @Override
            public void accept(JMSResponse response) {
                // noop
            }
        });
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 3
Source File: JMSPublisherConsumerIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
private void testMessage(String destinationName, MessageCreator messageCreator, ConsumerCallback responseChecker) {
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    AtomicBoolean callbackInvoked = new AtomicBoolean();

    try {
        jmsTemplate.send(destinationName, messageCreator);

        JMSConsumer consumer = new JMSConsumer((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        consumer.consume(destinationName, null, false, false, null, "UTF-8", response -> {
            callbackInvoked.set(true);
            responseChecker.accept(response);
        });

        assertTrue(callbackInvoked.get());
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 4
Source File: JMSPublisherConsumerTest.java    From solace-integration-guides with Apache License 2.0 6 votes vote down vote up
/**
 * At the moment the only two supported message types are TextMessage and
 * BytesMessage which is sufficient for the type if JMS use cases NiFi is
 * used. The may change to the point where all message types are supported
 * at which point this test will no be longer required.
 */
@Test(expected = IllegalStateException.class)
public void validateFailOnUnsupportedMessageType() throws Exception {
    final String destinationName = "testQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    jmsTemplate.send(destinationName, new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createObjectMessage();
        }
    });

    JMSConsumer consumer = new JMSConsumer(jmsTemplate, mock(ComponentLog.class));
    try {
        consumer.consume(destinationName, new ConsumerCallback() {
            @Override
            public void accept(JMSResponse response) {
                // noop
            }
        });
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 5
Source File: JMSPublisherConsumerTest.java    From solace-integration-guides with Apache License 2.0 6 votes vote down vote up
public void validateFailOnUnsupportedMessageTypeOverJNDI() throws Exception {
    final String destinationName = "testQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsJndiTemplateForDestination(false);

    jmsTemplate.send(destinationName, new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createObjectMessage();
        }
    });

    JMSConsumer consumer = new JMSConsumer(jmsTemplate, mock(ComponentLog.class));
    try {
        consumer.consume(destinationName, new ConsumerCallback() {
            @Override
            public void accept(JMSResponse response) {
                // noop
            }
        });
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 6
Source File: ActiveMQConnectionTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Test
public void connectionTest() {
    final String message = UUID.randomUUID().toString();
    final SjmsComponent sjms1 = context.getComponent("sjms-sjms-0-0", SjmsComponent.class);
    final SjmsComponent sjms2 = context.getComponent("sjms-sjms-0-1", SjmsComponent.class);

    Assertions.assertThat(sjms1).isNotEqualTo(sjms2);

    JmsTemplate subTemplate = new JmsTemplate(new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false&broker.brokerName=sub"));
    subTemplate.send("sub-" + testName.getMethodName(), session -> session.createTextMessage(message));

    JmsTemplate pubTemplate = new JmsTemplate(new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false&broker.brokerName=pub"));
    Object answer = pubTemplate.receive("pub-" + testName.getMethodName());

    Assertions.assertThat(answer).isInstanceOf(TextMessage.class);
    Assertions.assertThat(answer).hasFieldOrPropertyWithValue("text", message);
}
 
Example 7
Source File: JMSPublisherConsumerIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateConsumeWithCustomHeadersAndProperties() throws Exception {
    final String destinationName = "validateConsumeWithCustomHeadersAndProperties";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    try {
        jmsTemplate.send(destinationName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage message = session.createTextMessage("hello from the other side");
                message.setStringProperty("foo", "foo");
                message.setBooleanProperty("bar", false);
                message.setJMSReplyTo(session.createQueue("fooQueue"));
                return message;
            }
        });

        JMSConsumer consumer = new JMSConsumer((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        final AtomicBoolean callbackInvoked = new AtomicBoolean();
        consumer.consume(destinationName, null, false, false, null, "UTF-8", new ConsumerCallback() {
            @Override
            public void accept(JMSResponse response) {
                callbackInvoked.set(true);
                assertEquals("hello from the other side", new String(response.getMessageBody()));
                assertEquals("fooQueue", response.getMessageHeaders().get(JmsHeaders.REPLY_TO));
                assertEquals("foo", response.getMessageProperties().get("foo"));
                assertEquals("false", response.getMessageProperties().get("bar"));
            }
        });
        assertTrue(callbackInvoked.get());

    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 8
Source File: JmsTransactionManagerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testTransactionCommitWithMessageProducer() throws JMSException {
	Destination dest = new StubQueue();

	ConnectionFactory cf = mock(ConnectionFactory.class);
	Connection con = mock(Connection.class);
	Session session = mock(Session.class);
	MessageProducer producer = mock(MessageProducer.class);
	final Message message = mock(Message.class);

	given(cf.createConnection()).willReturn(con);
	given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
	given(session.createProducer(dest)).willReturn(producer);
	given(session.getTransacted()).willReturn(true);

	JmsTransactionManager tm = new JmsTransactionManager(cf);
	TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
	JmsTemplate jt = new JmsTemplate(cf);
	jt.send(dest, sess -> message);
	tm.commit(ts);

	verify(producer).send(message);
	verify(session).commit();
	verify(producer).close();
	verify(session).close();
	verify(con).close();
}
 
Example 9
Source File: MessageBasedJobManager.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
protected void sendMessage(final JobInfo job) {
    JmsTemplate actualJmsTemplate = (job instanceof HistoryJob) ? historyJmsTemplate : jmsTemplate;
    actualJmsTemplate.send(new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createTextMessage(job.getId());
        }
    });
}
 
Example 10
Source File: JmsUtil.java    From zheng with MIT License 5 votes vote down vote up
/**
 * 延迟发送对象消息
 * @param jmsTemplate
 * @param destination
 * @param objectMessage
 * @param delay
 */
public static void sendMessageDelay(JmsTemplate jmsTemplate, Destination destination, final Serializable objectMessage, final long delay) {
    jmsTemplate.send(destination, new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            ObjectMessage om = session.createObjectMessage(objectMessage);
            om.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
            om.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 1 * 1000);
            om.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
            return om;
        }
    });
}
 
Example 11
Source File: JmsUtil.java    From zheng with MIT License 5 votes vote down vote up
/**
 * 发送对象消息
 * @param jmsTemplate
 * @param destination
 * @param objectMessage
 */
public static void sendMessage(JmsTemplate jmsTemplate, Destination destination, final Serializable objectMessage) {
    jmsTemplate.send(destination, new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createObjectMessage(objectMessage);
        }
    });
}
 
Example 12
Source File: JmsUtil.java    From zheng with MIT License 5 votes vote down vote up
/**
 * 发送文本消息
 * @param jmsTemplate
 * @param destination
 * @param textMessage
 */
public static void sendMessage(JmsTemplate jmsTemplate, Destination destination, final String textMessage) {
    jmsTemplate.send(destination, new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createTextMessage(textMessage);
        }
    });
}
 
Example 13
Source File: ActiveMQSharedConnectionTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void sharedConnectionTest() {
    final String message = UUID.randomUUID().toString();
    final SjmsComponent sjms1 = context.getComponent("sjms-sjms-0-0", SjmsComponent.class);
    final SjmsComponent sjms2 = context.getComponent("sjms-sjms-0-1", SjmsComponent.class);

    Assertions.assertThat(sjms1).isEqualTo(sjms2);

    JmsTemplate template = new JmsTemplate(broker.createConnectionFactory());
    template.send("sub-" + testName.getMethodName(), session -> session.createTextMessage(message));
    Object answer = template.receive("pub-" + testName.getMethodName());

    Assertions.assertThat(answer).isInstanceOf(TextMessage.class);
    Assertions.assertThat(answer).hasFieldOrPropertyWithValue("text", message);
}
 
Example 14
Source File: ActiveMQSubscribeConnectorTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void subscribeTest() throws InterruptedException {
    final String message = UUID.randomUUID().toString();

    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived(message);

    JmsTemplate template = new JmsTemplate(broker.createConnectionFactory());
    template.send(testName.getMethodName(), session -> session.createTextMessage(message));

    mock.assertIsSatisfied();
}
 
Example 15
Source File: JMSPublisherConsumerTest.java    From solace-integration-guides with Apache License 2.0 5 votes vote down vote up
@Test
public void validateConsumeWithCustomHeadersAndPropertiesOverJNDI() throws Exception {
    final String destinationName = "testQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsJndiTemplateForDestination(false);

    jmsTemplate.send(destinationName, new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            TextMessage message = session.createTextMessage("hello from the other side");
            message.setStringProperty("foo", "foo");
            message.setBooleanProperty("bar", false);
            message.setJMSReplyTo(session.createQueue("fooQueue"));
            return message;
        }
    });

    JMSConsumer consumer = new JMSConsumer(jmsTemplate, mock(ComponentLog.class));
    final AtomicBoolean callbackInvoked = new AtomicBoolean();
    consumer.consume(destinationName, new ConsumerCallback() {
        @Override
        public void accept(JMSResponse response) {
            callbackInvoked.set(true);
            assertEquals("hello from the other side", new String(response.getMessageBody()));
            assertEquals("fooQueue", response.getMessageHeaders().get(JmsHeaders.REPLY_TO));
            assertEquals("foo", response.getMessageProperties().get("foo"));
            assertEquals("false", response.getMessageProperties().get("bar"));
        }
    });
    assertTrue(callbackInvoked.get());

    ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
 
Example 16
Source File: JMSPublisherConsumerTest.java    From solace-integration-guides with Apache License 2.0 5 votes vote down vote up
@Test
public void validateConsumeWithCustomHeadersAndProperties() throws Exception {
    final String destinationName = "testQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    jmsTemplate.send(destinationName, new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            TextMessage message = session.createTextMessage("hello from the other side");
            message.setStringProperty("foo", "foo");
            message.setBooleanProperty("bar", false);
            message.setJMSReplyTo(session.createQueue("fooQueue"));
            return message;
        }
    });

    JMSConsumer consumer = new JMSConsumer(jmsTemplate, mock(ComponentLog.class));
    final AtomicBoolean callbackInvoked = new AtomicBoolean();
    consumer.consume(destinationName, new ConsumerCallback() {
        @Override
        public void accept(JMSResponse response) {
            callbackInvoked.set(true);
            assertEquals("hello from the other side", new String(response.getMessageBody()));
            assertEquals("fooQueue", response.getMessageHeaders().get(JmsHeaders.REPLY_TO));
            assertEquals("foo", response.getMessageProperties().get("foo"));
            assertEquals("false", response.getMessageProperties().get("bar"));
        }
    });
    assertTrue(callbackInvoked.get());

    ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
 
Example 17
Source File: MQProducer.java    From paas with Apache License 2.0 5 votes vote down vote up
/**
 * 延时发送
 * @param message 内容,一般为JSON
 * @param time 时间(单位:ms)
 * @author jitwxs
 * @since 2018/5/8 21:08
 */
public void delaySend(Destination destination,String message, Long time) {
    // 得到jmsTemplate
    JmsTemplate jmsTemplate = jmsMessagingTemplate.getJmsTemplate();
    // 发送消息
    jmsTemplate.send(destination, session -> {
        TextMessage textMessage = session.createTextMessage(message);
        // 设置延时时间
        textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
        return textMessage;
    });
}
 
Example 18
Source File: JMSPublisherConsumerTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateConsumeWithCustomHeadersAndProperties() throws Exception {
    final String destinationName = "testQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    jmsTemplate.send(destinationName, new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            TextMessage message = session.createTextMessage("hello from the other side");
            message.setStringProperty("foo", "foo");
            message.setBooleanProperty("bar", false);
            message.setJMSReplyTo(session.createQueue("fooQueue"));
            return message;
        }
    });

    JMSConsumer consumer = new JMSConsumer(jmsTemplate, mock(ComponentLog.class));
    final AtomicBoolean callbackInvoked = new AtomicBoolean();
    consumer.consume(destinationName, new ConsumerCallback() {
        @Override
        public void accept(JMSResponse response) {
            callbackInvoked.set(true);
            assertEquals("hello from the other side", new String(response.getMessageBody()));
            assertEquals("fooQueue", response.getMessageHeaders().get(JmsHeaders.REPLY_TO));
            assertEquals("foo", response.getMessageProperties().get("foo"));
            assertEquals("false", response.getMessageProperties().get("bar"));
        }
    });
    assertTrue(callbackInvoked.get());

    ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
 
Example 19
Source File: TestProducer.java    From blog-sample with Apache License 2.0 5 votes vote down vote up
@Test
public void testProducer() {
    // 1、加载Spring容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-activemq.xml");
    // 2、获取jmsTemplate对象
    JmsTemplate jmsTemplate = ac.getBean(JmsTemplate.class);
    // 3、获取Destination对象(以Queue模式为例,如果要测试Topic更换下面注释即可)
    Destination destination = (Destination)ac.getBean("queueDestination");
    //Destination destination = (Destination)ac.getBean("topicDestination");
    // 4、发送消息
    jmsTemplate.send(destination, session -> session.createTextMessage("这是Spring与ActiveMQ整合的测试消息"));
}
 
Example 20
Source File: MQProducer.java    From blog-sample with Apache License 2.0 5 votes vote down vote up
/**
 * 延时发送
 * @param message 内容,一般为JSON
 * @param time 时间(单位:ms)
 * @author jitwxs
 * @since 2018/5/8 21:08
 */
public void delaySend(Destination destination,String message, Long time) {
    // 得到jmsTemplate
    JmsTemplate jmsTemplate = jmsMessagingTemplate.getJmsTemplate();
    // 发送消息
    jmsTemplate.send(destination, session -> {
        TextMessage textMessage = session.createTextMessage(message);
        // 设置延时时间【关键】
        textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
        return textMessage;
    });
}