Search in sources :

Example 1 with JMSConnectionFactoryProviderDefinition

use of org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition 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 JMSConnectionFactoryProviderDefinition

use of org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition 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 JMSConnectionFactoryProviderDefinition

use of org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition in project nifi by apache.

the class PublishJMSIT method validateSuccessfulPublishAndTransferToSuccessWithEL.

@Test(timeout = 10000)
public void validateSuccessfulPublishAndTransferToSuccessWithEL() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    final String destinationNameExpression = "${foo}Queue";
    final String destinationName = "fooQueue";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);
    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);
    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationNameExpression);
    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put(JmsHeaders.REPLY_TO, "cooQueue");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    // Run once but don't shut down because we want the Connection Factory left in tact so that we can use it.
    runner.run(1, false);
    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);
    JmsTemplate jmst = new JmsTemplate(cf);
    BytesMessage message = (BytesMessage) jmst.receive(destinationName);
    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(message);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals("cooQueue", ((Queue) message.getJMSReplyTo()).getQueueName());
    assertEquals("foo", message.getStringProperty("foo"));
    // Run once just so that we can trigger the shutdown of the Connection Factory
    runner.run(1, true, false);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) JmsTemplate(org.springframework.jms.core.JmsTemplate) JMSConnectionFactoryProviderDefinition(org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition) BytesMessage(javax.jms.BytesMessage) Test(org.junit.Test)

Example 4 with JMSConnectionFactoryProviderDefinition

use of org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition in project nifi by apache.

the class PublishJMSIT method validatePublishTextMessage.

@Test(timeout = 10000)
public void validatePublishTextMessage() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    final String destinationName = "validatePublishTextMessage";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);
    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);
    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationName);
    runner.setProperty(PublishJMS.MESSAGE_BODY, "text");
    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put(JmsHeaders.REPLY_TO, "cooQueue");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false);
    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);
    JmsTemplate jmst = new JmsTemplate(cf);
    Message message = jmst.receive(destinationName);
    assertTrue(message instanceof TextMessage);
    TextMessage textMessage = (TextMessage) message;
    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(textMessage);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals("cooQueue", ((Queue) message.getJMSReplyTo()).getQueueName());
    assertEquals("foo", message.getStringProperty("foo"));
    // Run once just so that we can trigger the shutdown of the Connection Factory
    runner.run(1, true, false);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MockFlowFile(org.apache.nifi.util.MockFlowFile) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) JmsTemplate(org.springframework.jms.core.JmsTemplate) JMSConnectionFactoryProviderDefinition(org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 5 with JMSConnectionFactoryProviderDefinition

use of org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition in project nifi by apache.

the class PublishJMSIT method validateSuccessfulPublishAndTransferToSuccess.

@Test(timeout = 10000)
public void validateSuccessfulPublishAndTransferToSuccess() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    final String destinationName = "validateSuccessfulPublishAndTransferToSuccess";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);
    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);
    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationName);
    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put(JmsHeaders.REPLY_TO, "cooQueue");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    // Run once but don't shut down because we want the Connection Factory left in tact so that we can use it.
    runner.run(1, false);
    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);
    JmsTemplate jmst = new JmsTemplate(cf);
    BytesMessage message = (BytesMessage) jmst.receive(destinationName);
    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(message);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals("cooQueue", ((Queue) message.getJMSReplyTo()).getQueueName());
    assertEquals("foo", message.getStringProperty("foo"));
    // Run once just so that we can trigger the shutdown of the Connection Factory
    runner.run(1, true, false);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) JmsTemplate(org.springframework.jms.core.JmsTemplate) JMSConnectionFactoryProviderDefinition(org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition) BytesMessage(javax.jms.BytesMessage) Test(org.junit.Test)

Aggregations

JMSConnectionFactoryProviderDefinition (org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition)6 TestRunner (org.apache.nifi.util.TestRunner)5 Test (org.junit.Test)5 JmsTemplate (org.springframework.jms.core.JmsTemplate)5 HashMap (java.util.HashMap)4 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 MockFlowFile (org.apache.nifi.util.MockFlowFile)4 BytesMessage (javax.jms.BytesMessage)3 ConnectionFactory (javax.jms.ConnectionFactory)2 CachingConnectionFactory (org.springframework.jms.connection.CachingConnectionFactory)2 Message (javax.jms.Message)1 TextMessage (javax.jms.TextMessage)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 UserCredentialsConnectionFactoryAdapter (org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter)1