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());
}
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();
}
}
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();
}
}
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);
}
}
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;
}
Aggregations