org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition Java Examples

The following examples show how to use org.apache.nifi.jms.cf.JMSConnectionFactoryProviderDefinition. 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: AbstractJMSProcessor.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * 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 void buildTargetResource(ProcessContext context) {
    if (this.targetResource == null) {
        JMSConnectionFactoryProviderDefinition cfProvider = context.getProperty(CF_SERVICE).asControllerService(JMSConnectionFactoryProviderDefinition.class);
        ConnectionFactory connectionFactory = cfProvider.getConnectionFactory();

        UserCredentialsConnectionFactoryAdapter cfCredentialsAdapter = new UserCredentialsConnectionFactoryAdapter();
        cfCredentialsAdapter.setTargetConnectionFactory(connectionFactory);
        cfCredentialsAdapter.setUsername(context.getProperty(USER).getValue());
        cfCredentialsAdapter.setPassword(context.getProperty(PASSWORD).getValue());

        this.cachingConnectionFactory = new CachingConnectionFactory(cfCredentialsAdapter);
        this.cachingConnectionFactory.setSessionCacheSize(Integer.parseInt(context.getProperty(SESSION_CACHE_SIZE).getValue()));

        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(this.cachingConnectionFactory);
        jmsTemplate.setPubSubDomain(TOPIC.equals(context.getProperty(DESTINATION_TYPE).getValue()));

        // set of properties that may be good candidates for exposure via configuration
        jmsTemplate.setReceiveTimeout(1000);

        this.targetResource = this.finishBuildingTargetResource(jmsTemplate, context);
    }
}
 
Example #2
Source File: PublishJMSTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateFailedPublishAndTransferToFailure() throws Exception {
    ConnectionFactory cf = mock(ConnectionFactory.class);

    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, "fooQueue");

    runner.enqueue("Hello Joe".getBytes());

    runner.run();
    Thread.sleep(200);

    assertTrue(runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).isEmpty());
    assertNotNull(runner.getFlowFilesForRelationship(PublishJMS.REL_FAILURE).get(0));
}
 
Example #3
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateFailedPublishAndTransferToFailure() throws Exception {
    ConnectionFactory cf = mock(ConnectionFactory.class);

    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, "validateFailedPublishAndTransferToFailure");

    runner.enqueue("Hello Joe".getBytes());

    runner.run();
    Thread.sleep(200);

    assertTrue(runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).isEmpty());
    assertNotNull(runner.getFlowFilesForRelationship(PublishJMS.REL_FAILURE).get(0));
}
 
Example #4
Source File: AbstractJMSProcessor.java    From solace-integration-guides with Apache License 2.0 6 votes vote down vote up
/**
 * 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 void buildTargetResource(ProcessContext context) {
    if (this.targetResource == null) {
        JMSConnectionFactoryProviderDefinition cfProvider = context.getProperty(CF_SERVICE).asControllerService(JMSConnectionFactoryProviderDefinition.class);
        ConnectionFactory connectionFactory = cfProvider.getConnectionFactory();

        UserCredentialsConnectionFactoryAdapter cfCredentialsAdapter = new UserCredentialsConnectionFactoryAdapter();
        cfCredentialsAdapter.setTargetConnectionFactory(connectionFactory);
        cfCredentialsAdapter.setUsername(context.getProperty(USER).getValue());
        cfCredentialsAdapter.setPassword(context.getProperty(PASSWORD).getValue());

        this.cachingConnectionFactory = new CachingConnectionFactory(cfCredentialsAdapter);
        this.cachingConnectionFactory.setSessionCacheSize(Integer.parseInt(context.getProperty(SESSION_CACHE_SIZE).getValue()));

        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(this.cachingConnectionFactory);
        jmsTemplate.setPubSubDomain(TOPIC.equals(context.getProperty(DESTINATION_TYPE).getValue()));

        // set of properties that may be good candidates for exposure via configuration
        jmsTemplate.setReceiveTimeout(1000);

        this.targetResource = this.finishBuildingTargetResource(jmsTemplate, context);
    }
}
 
Example #5
Source File: ConsumeJMSIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
private static TestRunner createNonSharedDurableConsumer(ActiveMQConnectionFactory cf, final String destinationName) {
    ConsumeJMS c1 = new ConsumeJMS();
    TestRunner c1Consumer = TestRunners.newTestRunner(c1);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);

    try {
        c1Consumer.addControllerService("cfProvider", cs);
    } catch (InitializationException e) {
        throw new IllegalStateException(e);
    }
    c1Consumer.enableControllerService(cs);

    c1Consumer.setProperty(ConsumeJMS.CF_SERVICE, "cfProvider");
    c1Consumer.setProperty(ConsumeJMS.DESTINATION, destinationName);
    c1Consumer.setProperty(ConsumeJMS.DESTINATION_TYPE, ConsumeJMS.TOPIC);
    c1Consumer.setProperty(ConsumeJMS.DURABLE_SUBSCRIBER, "true");
    c1Consumer.setProperty(ConsumeJMS.SUBSCRIPTION_NAME, "SubscriptionName");
    c1Consumer.setProperty(ConsumeJMS.SHARED_SUBSCRIBER, "false");
    c1Consumer.setProperty(ConsumeJMS.CLIENT_ID, "client1");
    return c1Consumer;
}
 
Example #6
Source File: ConsumeJMSIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10000)
public void whenExceptionIsRaisedTheProcessorShouldBeYielded() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(new ConsumeJMS());
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://invalidhost:9999?soTimeout=3");

    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);
    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);

    runner.setProperty(ConsumeJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(ConsumeJMS.DESTINATION, "foo");
    runner.setProperty(ConsumeJMS.DESTINATION_TYPE, ConsumeJMS.TOPIC);

    try {
        runner.run();
        fail("The test was implemented in a way this line should not be reached.");
    } catch (AssertionError e) {
    } finally {
        assertTrue("In case of an exception, the processor should be yielded.", ((MockProcessContext) runner.getProcessContext()).isYieldCalled());
    }
}
 
Example #7
Source File: PublishJMSTest.java    From solace-integration-guides with Apache License 2.0 6 votes vote down vote up
@Test
public void validateFailedPublishAndTransferToFailure() throws Exception {
    ConnectionFactory cf = mock(ConnectionFactory.class);

    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, "fooQueue");

    runner.enqueue("Hello Joe".getBytes());

    runner.run();
    Thread.sleep(200);

    assertTrue(runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).isEmpty());
    assertNotNull(runner.getFlowFilesForRelationship(PublishJMS.REL_FAILURE).get(0));
}
 
Example #8
Source File: PublishJMSTest.java    From solace-integration-guides with Apache License 2.0 6 votes vote down vote up
@Test
public void validateFailedPublishAndTransferToFailureOverJNDI() throws Exception {
    ConnectionFactory cf = mock(ConnectionFactory.class);

    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, "fooQueue");

    runner.enqueue("Hello Joe".getBytes());

    runner.run();
    Thread.sleep(200);

    assertTrue(runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).isEmpty());
    assertNotNull(runner.getFlowFilesForRelationship(PublishJMS.REL_FAILURE).get(0));
}
 
Example #9
Source File: PublishJMSTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSuccessfulPublishAndTransferToSuccess() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    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, destinationName);

    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);
    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"));
}
 
Example #10
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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"));

    runner.run(1, true, false); // Run once just so that we can trigger the shutdown of the Connection Factory
}
 
Example #11
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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);
    runner.run(1, false); // Run once but don't shut down because we want the Connection Factory left in tact so that we can use it.

    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"));

    runner.run(1, true, false); // Run once just so that we can trigger the shutdown of the Connection Factory
}
 
Example #12
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 #13
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 #14
Source File: ConsumeJMSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void testValidateErrorQueue(String destinationType, String errorQueue, boolean expectedValid) throws Exception {
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    try {
        TestRunner runner = TestRunners.newTestRunner(new ConsumeJMS());

        JMSConnectionFactoryProviderDefinition cfService = mock(JMSConnectionFactoryProviderDefinition.class);
        when(cfService.getIdentifier()).thenReturn("cfService");
        when(cfService.getConnectionFactory()).thenReturn(jmsTemplate.getConnectionFactory());

        runner.addControllerService("cfService", cfService);
        runner.enableControllerService(cfService);

        runner.setProperty(PublishJMS.CF_SERVICE, "cfService");
        runner.setProperty(ConsumeJMS.DESTINATION, "destination");
        runner.setProperty(ConsumeJMS.DESTINATION_TYPE, destinationType);
        if (errorQueue != null) {
            runner.setProperty(ConsumeJMS.ERROR_QUEUE, errorQueue);
        }

        if (expectedValid) {
            runner.assertValid();
        } else {
            runner.assertNotValid();
        }
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example #15
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 #16
Source File: AbstractJMSProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
@OnScheduled
public void setupConnectionFactoryProvider(final ProcessContext context) {
    if (context.getProperty(CF_SERVICE).isSet()) {
        connectionFactoryProvider = context.getProperty(CF_SERVICE).asControllerService(JMSConnectionFactoryProviderDefinition.class);
    } else if (context.getProperty(JndiJmsConnectionFactoryProperties.JNDI_CONNECTION_FACTORY_NAME).isSet()) {
        connectionFactoryProvider = new JndiJmsConnectionFactoryHandler(context, getLogger());
    } else if (context.getProperty(JMSConnectionFactoryProperties.JMS_CONNECTION_FACTORY_IMPL).isSet()) {
        connectionFactoryProvider = new JMSConnectionFactoryHandler(context, getLogger());
    } else {
        throw new ProcessException("No Connection Factory configured.");
    }
}
 
Example #17
Source File: ConsumeJMSTest.java    From solace-integration-guides 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);
    JMSPublisher sender = new JMSPublisher(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);

    ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
 
Example #18
Source File: PublishJMSTest.java    From solace-integration-guides with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSuccessfulPublishAndTransferToSuccessWithELOverJNDI() throws Exception {
    ActiveMQConnectionFactory cf = (ActiveMQConnectionFactory) CommonTest.buildJmsJndiConnectionFactory();

    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);
    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"));
}
 
Example #19
Source File: PublishJMSTest.java    From solace-integration-guides with Apache License 2.0 5 votes vote down vote up
@Test
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);
    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"));
}
 
Example #20
Source File: PublishJMSTest.java    From solace-integration-guides with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSuccessfulPublishAndTransferToSuccessOverJNDI() throws Exception {
    ActiveMQConnectionFactory cf = (ActiveMQConnectionFactory) CommonTest.buildJmsJndiConnectionFactory();

    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, destinationName);

    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);
    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"));
}
 
Example #21
Source File: PublishJMSTest.java    From solace-integration-guides with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSuccessfulPublishAndTransferToSuccess() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    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, destinationName);

    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);
    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"));
}
 
Example #22
Source File: ConsumeJMSTest.java    From localization_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);
    JMSPublisher sender = new JMSPublisher(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);

    ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
 
Example #23
Source File: PublishJMSTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
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);
    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"));
}
 
Example #24
Source File: ConsumeJMSTest.java    From solace-integration-guides with Apache License 2.0 4 votes vote down vote up
@Test
public void validateSuccessfulConsumeAndTransferToSuccessOverJNDI() {
    final String destinationName = "cooQueue";
    
    try {
        JmsTemplate jmsTemplate = CommonTest.buildJmsJndiTemplateForDestination(false);

        JMSPublisher sender = new JMSPublisher(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);

        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    } catch (Exception ex) {
        Logger.getLogger(ConsumeJMSTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example #25
Source File: ConsumeJMSIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * <p>
 * This test validates the connection resources are closed if the publisher is marked as invalid.
 * </p>
 * <p>
 * This tests validates the proper resources handling for TCP connections using ActiveMQ (the bug was discovered against ActiveMQ 5.x). In this test, using some ActiveMQ's classes is possible to
 * verify if an opened socket is closed. See <a href="https://issues.apache.org/jira/browse/NIFI-7034">NIFI-7034</a>.
 * </p>
 * @throws Exception
 *             any error related to the broker.
 */
@Test(timeout = 10000)
public void validateNIFI7034() throws Exception {
    class ConsumeJMSForNifi7034 extends ConsumeJMS {
        @Override
        protected void rendezvousWithJms(ProcessContext context, ProcessSession processSession, JMSConsumer consumer) throws ProcessException {
            super.rendezvousWithJms(context, processSession, consumer);
            consumer.setValid(false);
        }
    }
    BrokerService broker = new BrokerService();
    try {
        broker.setPersistent(false);
        broker.setBrokerName("nifi7034publisher");
        TransportConnector connector = broker.addConnector("tcp://127.0.0.1:0");
        int port = connector.getServer().getSocketAddress().getPort();
        broker.start();

        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("validateNIFI7034://127.0.0.1:" + port);
        final String destinationName = "nifi7034";
        final AtomicReference<TcpTransport> tcpTransport = new AtomicReference<TcpTransport>();
        TcpTransportFactory.registerTransportFactory("validateNIFI7034", new TcpTransportFactory() {
            @Override
            protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException {
                TcpTransport transport = super.createTcpTransport(wf, socketFactory, location, localLocation);
                tcpTransport.set(transport);
                return transport;
            }
        });

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

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

        try {
            runner.run();
            fail("Unit test implemented in a way this line must not be called");
        } catch (AssertionError e) {
            assertFalse("It is expected transport be closed. ", tcpTransport.get().isConnected());
        }
    } finally {
        if (broker != null) {
            broker.stop();
        }
    }
}
 
Example #26
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@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");
    attributes.put("test-attribute.type", "allowed1");
    attributes.put("test.attribute.type", "allowed2");
    attributes.put("test-attribute", "notAllowed1");
    attributes.put("jms.source.destination", "notAllowed2");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false); // Run once but don't shut down because we want the Connection Factory left in tact so that we can use it.

    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"));
    assertEquals("allowed1", message.getStringProperty("test-attribute.type"));
    assertEquals("allowed2", message.getStringProperty("test.attribute.type"));
    assertNull(message.getStringProperty("test-attribute"));
    assertNull(message.getStringProperty("jms.source.destination"));

    runner.run(1, true, false); // Run once just so that we can trigger the shutdown of the Connection Factory
}
 
Example #27
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 10000)
public void validatePublishPropertyTypes() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    final String destinationName = "validatePublishPropertyTypes";
    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("myboolean", "true");
    attributes.put("myboolean.type", "boolean");
    attributes.put("mybyte", "127");
    attributes.put("mybyte.type", "byte");
    attributes.put("myshort", "16384");
    attributes.put("myshort.type", "short");
    attributes.put("myinteger", "1544000");
    attributes.put("myinteger.type", "INTEGER"); // test upper case
    attributes.put("mylong", "9876543210");
    attributes.put("mylong.type", "long");
    attributes.put("myfloat", "3.14");
    attributes.put("myfloat.type", "float");
    attributes.put("mydouble", "3.14159265359");
    attributes.put("mydouble.type", "double");
    attributes.put("badtype", "3.14");
    attributes.put("badtype.type", "pi"); // pi not recognized as a type, so send as String
    attributes.put("badint", "3.14"); // value is not an integer
    attributes.put("badint.type", "integer");

    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false); // Run once but don't shut down because we want the Connection Factory left intact so that we can use it.

    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(true, message.getObjectProperty("foo") instanceof String);
    assertEquals("foo", message.getStringProperty("foo"));
    assertEquals(true, message.getObjectProperty("myboolean") instanceof Boolean);
    assertEquals(true, message.getBooleanProperty("myboolean"));
    assertEquals(true, message.getObjectProperty("mybyte") instanceof Byte);
    assertEquals(127, message.getByteProperty("mybyte"));
    assertEquals(true, message.getObjectProperty("myshort") instanceof Short);
    assertEquals(16384, message.getShortProperty("myshort"));
    assertEquals(true, message.getObjectProperty("myinteger") instanceof Integer);
    assertEquals(1544000, message.getIntProperty("myinteger"));
    assertEquals(true, message.getObjectProperty("mylong") instanceof Long);
    assertEquals(9876543210L, message.getLongProperty("mylong"));
    assertEquals(true, message.getObjectProperty("myfloat") instanceof Float);
    assertEquals(3.14F, message.getFloatProperty("myfloat"), 0.001F);
    assertEquals(true, message.getObjectProperty("mydouble") instanceof Double);
    assertEquals(3.14159265359D, message.getDoubleProperty("mydouble"), 0.00000000001D);
    assertEquals(true, message.getObjectProperty("badtype") instanceof String);
    assertEquals("3.14", message.getStringProperty("badtype"));
    assertFalse(message.propertyExists("badint"));

    runner.run(1, true, false); // Run once just so that we can trigger the shutdown of the Connection Factory
}
 
Example #28
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 10000)
public void validateRegexAndIllegalHeaders() 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");
    runner.setProperty(PublishJMS.ATTRIBUTES_AS_HEADERS_REGEX, "^((?!bar).)*$");
    runner.setProperty(PublishJMS.ALLOW_ILLEGAL_HEADER_CHARS, "true");

    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put("bar", "bar");
    attributes.put("test-header-with-hyphen", "value");
    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"));
    assertEquals("value", message.getStringProperty("test-header-with-hyphen"));
    assertNull(message.getStringProperty("bar"));

    runner.run(1, true, false); // Run once just so that we can trigger the shutdown of the Connection Factory
}
 
Example #29
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * <p>
 * This test validates the connection resources are closed if the publisher is marked as invalid.
 * </p>
 * <p>
 * This tests validates the proper resources handling for TCP connections using ActiveMQ (the bug was discovered against ActiveMQ 5.x). In this test, using some ActiveMQ's classes is possible to
 * verify if an opened socket is closed. See <a href="NIFI-7034">https://issues.apache.org/jira/browse/NIFI-7034</a>.
 * </p>
 * @throws Exception any error related to the broker.
 */
@Test(timeout = 10000)
public void validateNIFI7034() throws Exception {
    class PublishJmsForNifi7034 extends PublishJMS {
        @Override
        protected void rendezvousWithJms(ProcessContext context, ProcessSession processSession, JMSPublisher publisher) throws ProcessException {
            super.rendezvousWithJms(context, processSession, publisher);
            publisher.setValid(false);
        }
    }
    BrokerService broker = new BrokerService();
    try {
        broker.setPersistent(false);
        broker.setBrokerName("nifi7034publisher");
        TransportConnector connector = broker.addConnector("tcp://127.0.0.1:0");
        int port = connector.getServer().getSocketAddress().getPort();
        broker.start();

        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("validateNIFI7034://127.0.0.1:" + port);
        final String destinationName = "nifi7034";
        final AtomicReference<TcpTransport> tcpTransport = new AtomicReference<TcpTransport>();
        TcpTransportFactory.registerTransportFactory("validateNIFI7034", new TcpTransportFactory() {
            @Override
            protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException {
                TcpTransport transport = super.createTcpTransport(wf, socketFactory, location, localLocation);
                tcpTransport.set(transport);
                return transport;
            }
        });

        TestRunner runner = TestRunners.newTestRunner(new PublishJmsForNifi7034());
        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.DESTINATION_TYPE, PublishJMS.TOPIC);

        runner.enqueue("hi".getBytes());
        runner.run();
        assertFalse("It is expected transport be closed. ", tcpTransport.get().isConnected());
    } finally {
        if (broker != null) {
            broker.stop();
        }
    }
}