Search in sources :

Example 1 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project nifi by apache.

the class AbstractJMSProcessor method buildTargetResource.

/**
 * This method essentially performs initialization of this Processor by
 * obtaining an instance of the {@link ConnectionFactory} from the
 * {@link JMSConnectionFactoryProvider} (ControllerService) and performing a
 * series of {@link ConnectionFactory} adaptations which eventually results
 * in an instance of the {@link CachingConnectionFactory} used to construct
 * {@link JmsTemplate} used by this Processor.
 */
private T buildTargetResource(ProcessContext context) {
    final JMSConnectionFactoryProviderDefinition cfProvider = context.getProperty(CF_SERVICE).asControllerService(JMSConnectionFactoryProviderDefinition.class);
    final ConnectionFactory connectionFactory = cfProvider.getConnectionFactory();
    final UserCredentialsConnectionFactoryAdapter cfCredentialsAdapter = new UserCredentialsConnectionFactoryAdapter();
    cfCredentialsAdapter.setTargetConnectionFactory(connectionFactory);
    cfCredentialsAdapter.setUsername(context.getProperty(USER).getValue());
    cfCredentialsAdapter.setPassword(context.getProperty(PASSWORD).getValue());
    final CachingConnectionFactory cachingFactory = new CachingConnectionFactory(cfCredentialsAdapter);
    String clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue();
    if (clientId != null) {
        clientId = clientId + "-" + clientIdCounter.getAndIncrement();
        cachingFactory.setClientId(clientId);
    }
    JmsTemplate jmsTemplate = new JmsTemplate();
    jmsTemplate.setConnectionFactory(cachingFactory);
    jmsTemplate.setPubSubDomain(TOPIC.equals(context.getProperty(DESTINATION_TYPE).getValue()));
    // set of properties that may be good candidates for exposure via configuration
    jmsTemplate.setReceiveTimeout(1000);
    return finishBuildingJmsWorker(cachingFactory, jmsTemplate, context);
}
Also used : CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) JMSConnectionFactoryProviderDefinition(org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition) UserCredentialsConnectionFactoryAdapter(org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter)

Example 2 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project nifi by apache.

the class ConsumeJMSIT method validateSuccessfulConsumeAndTransferToSuccess.

@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.assertContentEquals("Hey dude!".getBytes());
        String sourceDestination = successFF.getAttribute(ConsumeJMS.JMS_SOURCE_DESTINATION_NAME);
        assertNotNull(sourceDestination);
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) JMSConnectionFactoryProviderDefinition(org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition) ComponentLog(org.apache.nifi.logging.ComponentLog) Test(org.junit.Test)

Example 3 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project nifi by apache.

the class JMSPublisherConsumerIT method validateConsumeWithCustomHeadersAndProperties.

@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, 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();
    }
}
Also used : TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) JMSResponse(org.apache.nifi.jms.processors.JMSConsumer.JMSResponse) ConsumerCallback(org.apache.nifi.jms.processors.JMSConsumer.ConsumerCallback) JMSException(javax.jms.JMSException) ComponentLog(org.apache.nifi.logging.ComponentLog) MessageCreator(org.springframework.jms.core.MessageCreator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 4 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project nifi by apache.

the class JMSPublisherConsumerIT method validateMessageRedeliveryWhenNotAcked.

@Test(timeout = 10000)
public void validateMessageRedeliveryWhenNotAcked() throws Exception {
    String destinationName = "validateMessageRedeliveryWhenNotAcked";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);
    try {
        JMSPublisher publisher = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        publisher.publish(destinationName, "1".getBytes(StandardCharsets.UTF_8));
        publisher.publish(destinationName, "2".getBytes(StandardCharsets.UTF_8));
        JMSConsumer consumer = new JMSConsumer((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        final AtomicBoolean callbackInvoked = new AtomicBoolean();
        try {
            consumer.consume(destinationName, false, false, null, "UTF-8", new ConsumerCallback() {

                @Override
                public void accept(JMSResponse response) {
                    callbackInvoked.set(true);
                    assertEquals("1", new String(response.getMessageBody()));
                    throw new RuntimeException("intentional to avoid explicit ack");
                }
            });
        } catch (Exception e) {
        // expected
        }
        assertTrue(callbackInvoked.get());
        callbackInvoked.set(false);
        // should receive the same message, but will process it successfully
        while (!callbackInvoked.get()) {
            consumer.consume(destinationName, false, false, null, "UTF-8", new ConsumerCallback() {

                @Override
                public void accept(JMSResponse response) {
                    if (response == null) {
                        return;
                    }
                    callbackInvoked.set(true);
                    assertEquals("1", new String(response.getMessageBody()));
                }
            });
        }
        assertTrue(callbackInvoked.get());
        callbackInvoked.set(false);
        // receiving next message and fail again
        try {
            while (!callbackInvoked.get()) {
                consumer.consume(destinationName, false, false, null, "UTF-8", new ConsumerCallback() {

                    @Override
                    public void accept(JMSResponse response) {
                        if (response == null) {
                            return;
                        }
                        callbackInvoked.set(true);
                        assertEquals("2", new String(response.getMessageBody()));
                        throw new RuntimeException("intentional to avoid explicit ack");
                    }
                });
            }
        } catch (Exception e) {
        // ignore
        }
        assertTrue(callbackInvoked.get());
        callbackInvoked.set(false);
        // should receive the same message, but will process it successfully
        try {
            while (!callbackInvoked.get()) {
                consumer.consume(destinationName, false, false, null, "UTF-8", new ConsumerCallback() {

                    @Override
                    public void accept(JMSResponse response) {
                        if (response == null) {
                            return;
                        }
                        callbackInvoked.set(true);
                        assertEquals("2", new String(response.getMessageBody()));
                    }
                });
            }
        } catch (Exception e) {
        // ignore
        }
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JMSResponse(org.apache.nifi.jms.processors.JMSConsumer.JMSResponse) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) ConsumerCallback(org.apache.nifi.jms.processors.JMSConsumer.ConsumerCallback) ComponentLog(org.apache.nifi.logging.ComponentLog) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 5 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project nifi by apache.

the class JMSPublisherConsumerIT method validateBytesConvertedToBytesMessageOnSend.

@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();
    }
}
Also used : TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) BytesMessage(javax.jms.BytesMessage) ComponentLog(org.apache.nifi.logging.ComponentLog) Test(org.junit.Test)

Aggregations

CachingConnectionFactory (org.springframework.jms.connection.CachingConnectionFactory)33 Test (org.junit.Test)21 JmsTemplate (org.springframework.jms.core.JmsTemplate)20 TextMessage (javax.jms.TextMessage)15 Message (javax.jms.Message)11 GenericMessage (org.springframework.messaging.support.GenericMessage)11 MessageCreator (org.springframework.jms.core.MessageCreator)10 BeanFactory (org.springframework.beans.factory.BeanFactory)9 Destination (javax.jms.Destination)8 ComponentLog (org.apache.nifi.logging.ComponentLog)7 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)6 JmsChannelFactoryBean (org.springframework.integration.jms.config.JmsChannelFactoryBean)6 ConnectionFactory (javax.jms.ConnectionFactory)5 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)5 ArrayList (java.util.ArrayList)4 ExecutorService (java.util.concurrent.ExecutorService)4 Session (javax.jms.Session)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Executors (java.util.concurrent.Executors)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3