use of javax.jms.MessageProducer in project beam by apache.
the class JmsIOTest method testReadMessages.
@Test
public void testReadMessages() throws Exception {
// produce message
Connection connection = connectionFactory.createConnection(USERNAME, PASSWORD);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(session.createQueue(QUEUE));
TextMessage message = session.createTextMessage("This Is A Test");
producer.send(message);
producer.send(message);
producer.send(message);
producer.send(message);
producer.send(message);
producer.send(message);
producer.close();
session.close();
connection.close();
// read from the queue
PCollection<JmsRecord> output = pipeline.apply(JmsIO.read().withConnectionFactory(connectionFactory).withQueue(QUEUE).withUsername(USERNAME).withPassword(PASSWORD).withMaxNumRecords(5));
PAssert.thatSingleton(output.apply("Count", Count.<JmsRecord>globally())).isEqualTo(new Long(5));
pipeline.run();
connection = connectionFactory.createConnection(USERNAME, PASSWORD);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(session.createQueue(QUEUE));
Message msg = consumer.receiveNoWait();
assertNull(msg);
}
use of javax.jms.MessageProducer in project spring-framework by spring-projects.
the class JmsInvokerClientInterceptor method doExecuteRequest.
/**
* Actually execute the given request, sending the invoker request message
* to the specified target queue and waiting for a corresponding response.
* <p>The default implementation is based on standard JMS send/receive,
* using a {@link javax.jms.TemporaryQueue} for receiving the response.
* @param session the JMS Session to use
* @param queue the resolved target Queue to send to
* @param requestMessage the JMS Message to send
* @return the RemoteInvocationResult object
* @throws JMSException in case of JMS failure
*/
protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
TemporaryQueue responseQueue = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
try {
responseQueue = session.createTemporaryQueue();
producer = session.createProducer(queue);
consumer = session.createConsumer(responseQueue);
requestMessage.setJMSReplyTo(responseQueue);
producer.send(requestMessage);
long timeout = getReceiveTimeout();
return (timeout > 0 ? consumer.receive(timeout) : consumer.receive());
} finally {
JmsUtils.closeMessageConsumer(consumer);
JmsUtils.closeMessageProducer(producer);
if (responseQueue != null) {
responseQueue.delete();
}
}
}
use of javax.jms.MessageProducer in project spring-framework by spring-projects.
the class JmsInvokerServiceExporter method writeRemoteInvocationResult.
/**
* Send the given RemoteInvocationResult as a JMS message to the originator.
* @param requestMessage current request message
* @param session the JMS Session to use
* @param result the RemoteInvocationResult object
* @throws javax.jms.JMSException if thrown by trying to send the message
*/
protected void writeRemoteInvocationResult(Message requestMessage, Session session, RemoteInvocationResult result) throws JMSException {
Message response = createResponseMessage(requestMessage, session, result);
MessageProducer producer = session.createProducer(requestMessage.getJMSReplyTo());
try {
producer.send(response);
} finally {
JmsUtils.closeMessageProducer(producer);
}
}
use of javax.jms.MessageProducer in project spring-framework by spring-projects.
the class JmsTemplate method doSend.
/**
* Send the given JMS message.
* @param session the JMS Session to operate on
* @param destination the JMS Destination to send to
* @param messageCreator callback to create a JMS Message
* @throws JMSException if thrown by JMS API methods
*/
protected void doSend(Session session, Destination destination, MessageCreator messageCreator) throws JMSException {
Assert.notNull(messageCreator, "MessageCreator must not be null");
MessageProducer producer = createProducer(session, destination);
try {
Message message = messageCreator.createMessage(session);
if (logger.isDebugEnabled()) {
logger.debug("Sending created message: " + message);
}
doSend(producer, message);
// Check commit - avoid commit call within a JTA transaction.
if (session.getTransacted() && isSessionLocallyTransacted(session)) {
// Transacted session created by this template -> commit.
JmsUtils.commitIfNecessary(session);
}
} finally {
JmsUtils.closeMessageProducer(producer);
}
}
use of javax.jms.MessageProducer in project spring-framework by spring-projects.
the class JmsTemplateTests method testProducerCallbackWithIdAndTimestampDisabled.
@Test
public void testProducerCallbackWithIdAndTimestampDisabled() throws Exception {
JmsTemplate template = createTemplate();
template.setConnectionFactory(connectionFactory);
template.setMessageIdEnabled(false);
template.setMessageTimestampEnabled(false);
MessageProducer messageProducer = mock(MessageProducer.class);
given(session.createProducer(null)).willReturn(messageProducer);
given(messageProducer.getPriority()).willReturn(4);
template.execute(new ProducerCallback<Void>() {
@Override
public Void doInJms(Session session, MessageProducer producer) throws JMSException {
session.getTransacted();
producer.getPriority();
return null;
}
});
verify(messageProducer).setDisableMessageID(true);
verify(messageProducer).setDisableMessageTimestamp(true);
verify(messageProducer).close();
verify(session).close();
verify(connection).close();
}
Aggregations