Search in sources :

Example 66 with JMSException

use of javax.jms.JMSException in project nifi by apache.

the class TestPutJMS method testPutCommitRoutesToFailure.

@Test
public void testPutCommitRoutesToFailure() throws JMSException, NoSuchFieldException, IllegalAccessException {
    final PutJMS putJMS = spy(new PutJMS());
    final TestRunner runnerPut = TestRunners.newTestRunner(putJMS);
    runnerPut.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER);
    runnerPut.setProperty(JmsProperties.URL, TEST_URL);
    runnerPut.setProperty(JmsProperties.DESTINATION_TYPE, TEST_DEST_TYPE);
    runnerPut.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix());
    final ProcessContext context = runnerPut.getProcessContext();
    final Queue<WrappedMessageProducer> wrappedMessageProducerQueue = (Queue) spy(new LinkedBlockingQueue<>());
    injectFieldValue(PutJMS.class, putJMS, "producerQueue", wrappedMessageProducerQueue);
    final WrappedMessageProducer wrappedMessageProducer = spy(JmsFactory.createMessageProducer(context, true));
    final MessageProducer messageProducer = spy(wrappedMessageProducer.getProducer());
    final Connection connection = JmsFactory.createConnection(context);
    final Session jmsSession = spy(JmsFactory.createSession(context, connection, true));
    doAnswer(new Answer<WrappedMessageProducer>() {

        @Override
        public WrappedMessageProducer answer(InvocationOnMock invocationOnMock) {
            return wrappedMessageProducer;
        }
    }).when(wrappedMessageProducerQueue).poll();
    doAnswer(new Answer<MessageProducer>() {

        @Override
        public MessageProducer answer(InvocationOnMock invocationOnMock) {
            return messageProducer;
        }
    }).when(wrappedMessageProducer).getProducer();
    doAnswer(new Answer<Session>() {

        @Override
        public Session answer(InvocationOnMock invocationOnMock) {
            return jmsSession;
        }
    }).when(wrappedMessageProducer).getSession();
    doThrow(new JMSException("force commit to fail")).when(jmsSession).commit();
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("filename", "file1.txt");
    runnerPut.enqueue("putCommitRoutesToFailure".getBytes(), attributes);
    runnerPut.run();
    assertEquals(0, runnerPut.getFlowFilesForRelationship(PutJMS.REL_SUCCESS).size());
    assertEquals(1, runnerPut.getFlowFilesForRelationship(PutJMS.REL_FAILURE).size());
    final List<MockFlowFile> flowFilesFail = runnerPut.getFlowFilesForRelationship(PutJMS.REL_FAILURE);
    assertEquals(1, flowFilesFail.size());
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ProcessContext(org.apache.nifi.processor.ProcessContext) MockFlowFile(org.apache.nifi.util.MockFlowFile) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MessageProducer(javax.jms.MessageProducer) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(java.util.Queue) Session(javax.jms.Session) Test(org.junit.Test)

Example 67 with JMSException

use of javax.jms.JMSException 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 68 with JMSException

use of javax.jms.JMSException in project nifi by apache.

the class JMSPublisherConsumerIT method validateFailOnUnsupportedMessageType.

/**
 * 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, false, false, null, "UTF-8", new ConsumerCallback() {

            @Override
            public void accept(JMSResponse response) {
            // noop
            }
        });
    } 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) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) ConsumerCallback(org.apache.nifi.jms.processors.JMSConsumer.ConsumerCallback) JMSException(javax.jms.JMSException) ComponentLog(org.apache.nifi.logging.ComponentLog) MessageCreator(org.springframework.jms.core.MessageCreator) Session(javax.jms.Session) Test(org.junit.Test)

Example 69 with JMSException

use of javax.jms.JMSException in project sling by apache.

the class JMSQueueManager method add.

/**
     * Add a message to the queue. The message is added to the queue transactionally and auto acknowledged.
     * @param name the name of the queue.
     * @param message the message to post to the queue.
     */
@Override
public void add(@Nonnull Types.QueueName name, @Nonnull Map<String, Object> message) {
    Session session = null;
    try {
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        //TODO Instead of copy do addition at JSON writer level
        Map<String, Object> msgCopy = new HashMap<>(message);
        // set the number of retries to 0.
        msgCopy.put(NRETRIES, 0L);
        TextMessage textMessage = session.createTextMessage(Json.toJson(msgCopy));
        textMessage.setJMSType(JMSMessageTypes.JSON.toString());
        LOGGER.info("Sending to {} message {} ", name, textMessage);
        session.createProducer(session.createQueue(name.toString())).send(textMessage);
        session.commit();
        session.close();
    } catch (JMSException e) {
        LOGGER.error("Unable to send message to queue " + name, e);
        close(session);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 70 with JMSException

use of javax.jms.JMSException in project Protocol-Adapter-OSLP by OSGP.

the class DeviceRequestMessageProcessorMap method getMessageProcessor.

@Override
public MessageProcessor getMessageProcessor(final ObjectMessage message) throws JMSException {
    if (message.getJMSType() == null) {
        LOGGER.error("Unknown message type: {}", message.getJMSType());
        throw new JMSException("Unknown message type");
    }
    final DeviceRequestMessageType messageType = DeviceRequestMessageType.valueOf(message.getJMSType());
    if (messageType.name() == null) {
        LOGGER.error("No message processor found for message type: {}", message.getJMSType());
        throw new JMSException("Unknown message processor");
    }
    final MessageProcessor messageProcessor = this.messageProcessors.get(messageType.ordinal());
    if (messageProcessor == null) {
        throw new IllegalArgumentException("Message type is not supported: " + message.getJMSType());
    }
    return messageProcessor;
}
Also used : MessageProcessor(com.alliander.osgp.shared.infra.jms.MessageProcessor) JMSException(javax.jms.JMSException)

Aggregations

JMSException (javax.jms.JMSException)1094 Message (javax.jms.Message)355 Test (org.junit.Test)335 Session (javax.jms.Session)309 TextMessage (javax.jms.TextMessage)302 Connection (javax.jms.Connection)271 MessageProducer (javax.jms.MessageProducer)169 MessageConsumer (javax.jms.MessageConsumer)156 Destination (javax.jms.Destination)105 ObjectMessage (javax.jms.ObjectMessage)100 Queue (javax.jms.Queue)100 MapMessage (javax.jms.MapMessage)70 ConnectionFactory (javax.jms.ConnectionFactory)68 CountDownLatch (java.util.concurrent.CountDownLatch)64 IOException (java.io.IOException)62 BytesMessage (javax.jms.BytesMessage)59 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)54 MessageListener (javax.jms.MessageListener)49 NamingException (javax.naming.NamingException)44 MessageFormatException (javax.jms.MessageFormatException)43