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

The following examples show how to use org.springframework.jms.core.JmsTemplate#getConnectionFactory() . 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
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 2
Source File: JMSPublisherConsumerIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateBytesConvertedToBytesMessageOnSend() throws Exception {
    final String destinationName = "validateBytesConvertedToBytesMessageOnSend";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    try {
        JMSPublisher publisher = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        publisher.publish(destinationName, "hellomq".getBytes());

        Message receivedMessage = jmsTemplate.receive(destinationName);
        assertTrue(receivedMessage instanceof BytesMessage);
        byte[] bytes = new byte[7];
        ((BytesMessage) receivedMessage).readBytes(bytes);
        assertEquals("hellomq", new String(bytes));
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 3
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 4
Source File: ConsumeJMSManualTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
private void send(MessageCreator messageCreator) throws Exception {
    final String  destinationName = "TEST";

    ConnectionFactory activeMqConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    final ConnectionFactory connectionFactory = new CachingConnectionFactory(activeMqConnectionFactory);

    JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
    jmsTemplate.setPubSubDomain(false);
    jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    jmsTemplate.setReceiveTimeout(10L);

    try {
        JMSPublisher sender = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));

        sender.jmsTemplate.send(destinationName, messageCreator);
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 5
Source File: ConsumeJMSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSuccessfulConsumeAndTransferToSuccess() throws Exception {
    final String destinationName = "cooQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);
    try {
        JMSPublisher sender = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        final Map<String, String> senderAttributes = new HashMap<>();
        senderAttributes.put("filename", "message.txt");
        senderAttributes.put("attribute_from_sender", "some value");
        sender.publish(destinationName, "Hey dude!".getBytes(), senderAttributes);
        TestRunner runner = TestRunners.newTestRunner(new ConsumeJMS());
        JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
        when(cs.getIdentifier()).thenReturn("cfProvider");
        when(cs.getConnectionFactory()).thenReturn(jmsTemplate.getConnectionFactory());
        runner.addControllerService("cfProvider", cs);
        runner.enableControllerService(cs);

        runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
        runner.setProperty(ConsumeJMS.DESTINATION, destinationName);
        runner.setProperty(ConsumeJMS.DESTINATION_TYPE, ConsumeJMS.QUEUE);

        runner.run(1, false);
        //
        final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
        assertNotNull(successFF);
        successFF.assertAttributeExists(JmsHeaders.DESTINATION);
        successFF.assertAttributeEquals(JmsHeaders.DESTINATION, destinationName);
        successFF.assertAttributeExists("filename");
        successFF.assertAttributeEquals("filename", "message.txt");
        successFF.assertAttributeExists("attribute_from_sender");
        successFF.assertAttributeEquals("attribute_from_sender", "some value");
        successFF.assertAttributeExists("jms.messagetype");
        successFF.assertAttributeEquals("jms.messagetype", "BytesMessage");
        successFF.assertContentEquals("Hey dude!".getBytes());
        String sourceDestination = successFF.getAttribute(ConsumeJMS.JMS_SOURCE_DESTINATION_NAME);
        assertNotNull(sourceDestination);
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 6
Source File: ConsumeJMSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsupportedMessage() throws Exception {
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);
    try {
        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

        JMSPublisher sender = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));

        sender.jmsTemplate.send("testMapMessage", __ -> createUnsupportedMessage("unsupportedMessagePropertyKey", "unsupportedMessagePropertyValue"));

        TestRunner runner = TestRunners.newTestRunner(new ConsumeJMS());
        JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
        when(cs.getIdentifier()).thenReturn("cfProvider");
        when(cs.getConnectionFactory()).thenReturn(jmsTemplate.getConnectionFactory());
        runner.addControllerService("cfProvider", cs);
        runner.enableControllerService(cs);

        runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
        runner.setProperty(ConsumeJMS.DESTINATION, "testMapMessage");
        runner.setProperty(ConsumeJMS.ERROR_QUEUE, "errorQueue");
        runner.setProperty(ConsumeJMS.DESTINATION_TYPE, ConsumeJMS.QUEUE);
        runner.run(1, false);

        JmsTemplate jmst = new JmsTemplate(cf);
        Message message = jmst.receive("errorQueue");

        assertNotNull(message);
        assertEquals(message.getStringProperty("unsupportedMessagePropertyKey"), "unsupportedMessagePropertyValue");
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 7
Source File: ConsumeJMSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void testMessageTypeAttribute(String destinationName, final MessageCreator messageCreator, String expectedJmsMessageTypeAttribute) throws Exception {
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);
    try {
        JMSPublisher sender = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));

        sender.jmsTemplate.send(destinationName, messageCreator);

        TestRunner runner = TestRunners.newTestRunner(new ConsumeJMS());
        JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
        when(cs.getIdentifier()).thenReturn("cfProvider");
        when(cs.getConnectionFactory()).thenReturn(jmsTemplate.getConnectionFactory());
        runner.addControllerService("cfProvider", cs);
        runner.enableControllerService(cs);

        runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
        runner.setProperty(ConsumeJMS.DESTINATION, destinationName);
        runner.setProperty(ConsumeJMS.DESTINATION_TYPE, ConsumeJMS.QUEUE);
        runner.run(1, false);
        //
        final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
        assertNotNull(successFF);

        successFF.assertAttributeExists(ConsumeJMS.JMS_MESSAGETYPE);
        successFF.assertAttributeEquals(ConsumeJMS.JMS_MESSAGETYPE, expectedJmsMessageTypeAttribute);
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 8
Source File: JMSPublisherConsumerIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateJmsHeadersAndPropertiesAreTransferredFromFFAttributes() throws Exception {
    final String destinationName = "validateJmsHeadersAndPropertiesAreTransferredFromFFAttributes";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    try {
        JMSPublisher publisher = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        Map<String, String> flowFileAttributes = new HashMap<>();
        flowFileAttributes.put("foo", "foo");
        flowFileAttributes.put("hyphen-property", "value");
        flowFileAttributes.put("fullstop.property", "value");
        flowFileAttributes.put(JmsHeaders.REPLY_TO, "myTopic");
        flowFileAttributes.put(JmsHeaders.DELIVERY_MODE, "1");
        flowFileAttributes.put(JmsHeaders.PRIORITY, "1");
        flowFileAttributes.put(JmsHeaders.EXPIRATION, "never"); // value expected to be integer, make sure non-integer doesn't cause problems
        publisher.publish(destinationName, "hellomq".getBytes(), flowFileAttributes);

        Message receivedMessage = jmsTemplate.receive(destinationName);
        assertTrue(receivedMessage instanceof BytesMessage);
        assertEquals("foo", receivedMessage.getStringProperty("foo"));
        assertTrue(receivedMessage.propertyExists("hyphen-property"));
        assertTrue(receivedMessage.propertyExists("fullstop.property"));
        assertTrue(receivedMessage.getJMSReplyTo() instanceof Topic);
        assertEquals(1, receivedMessage.getJMSDeliveryMode());
        assertEquals(1, receivedMessage.getJMSPriority());
        assertEquals("myTopic", ((Topic) receivedMessage.getJMSReplyTo()).getTopicName());

    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 9
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 10
Source File: JMSPublisherConsumerIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testMultipleThreads() throws Exception {
    String destinationName = "testMultipleThreads";
    JmsTemplate publishTemplate = CommonTest.buildJmsTemplateForDestination(false);
    final CountDownLatch consumerTemplateCloseCount = new CountDownLatch(4);

    try {
        JMSPublisher publisher = new JMSPublisher((CachingConnectionFactory) publishTemplate.getConnectionFactory(), publishTemplate, mock(ComponentLog.class));
        for (int i = 0; i < 4000; i++) {
            publisher.publish(destinationName, String.valueOf(i).getBytes(StandardCharsets.UTF_8));
        }

        final AtomicInteger msgCount = new AtomicInteger(0);

        final ConsumerCallback callback = new ConsumerCallback() {
            @Override
            public void accept(JMSResponse response) {
                msgCount.incrementAndGet();
            }
        };

        final Thread[] threads = new Thread[4];
        for (int i = 0; i < 4; i++) {
            final Thread t = new Thread(() -> {
                JmsTemplate consumeTemplate = CommonTest.buildJmsTemplateForDestination(false);

                try {
                    JMSConsumer consumer = new JMSConsumer((CachingConnectionFactory) consumeTemplate.getConnectionFactory(), consumeTemplate, mock(ComponentLog.class));

                    for (int j = 0; j < 1000 && msgCount.get() < 4000; j++) {
                        consumer.consume(destinationName, null, false, false, null, "UTF-8", callback);
                    }
                } finally {
                    ((CachingConnectionFactory) consumeTemplate.getConnectionFactory()).destroy();
                    consumerTemplateCloseCount.countDown();
                }
            });

            threads[i] = t;
            t.start();
        }

        int iterations = 0;
        while (msgCount.get() < 4000) {
            Thread.sleep(10L);
            if (++iterations % 100 == 0) {
                System.out.println(msgCount.get() + " messages received so far");
            }
        }
    } finally {
        ((CachingConnectionFactory) publishTemplate.getConnectionFactory()).destroy();

        consumerTemplateCloseCount.await();
    }
}