use of javax.jms.MessageProducer in project wildfly by wildfly.
the class HelloBean method sendMessage.
public String sendMessage() throws Exception {
String destinationName = "java:jboss/exported/queue/TestQueue";
Context ic = null;
ConnectionFactory cf = null;
Connection connection = null;
try {
ic = getInitialContext();
cf = (ConnectionFactory) ic.lookup("java:/ConnectionFactory");
Queue queue = (Queue) ic.lookup(destinationName);
connection = cf.createConnection("guest", "guest");
// we need to start connection for consumer
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(queue);
TextMessage message = session.createTextMessage("hello goodbye");
TemporaryQueue replyQueue = session.createTemporaryQueue();
message.setJMSReplyTo(replyQueue);
sender.send(message);
MessageConsumer consumer = session.createConsumer(replyQueue);
TextMessage replyMsg = (TextMessage) consumer.receive(5000);
log.trace("Message received:" + replyMsg);
return replyMsg.getText();
} finally {
if (ic != null) {
try {
ic.close();
} catch (Exception ignore) {
}
}
closeConnection(connection);
}
}
use of javax.jms.MessageProducer in project wildfly by wildfly.
the class RunAsTestCaseEJBMDB method testSendMessage.
@Test
public void testSendMessage() throws Exception {
ConnectionFactory cf = null;
Connection connection = null;
Session session = null;
try {
cf = (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");
Queue queue = (Queue) initialContext.lookup(QUEUE_NAME);
connection = cf.createConnection("guest", "guest");
//for consumer we need to start connection
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(queue);
TemporaryQueue replyQueue = session.createTemporaryQueue();
TextMessage message = session.createTextMessage("hello goodbye");
message.setJMSReplyTo(replyQueue);
sender.send(message);
log.trace("testSendMessage(): Message sent!");
MessageConsumer consumer = session.createConsumer(replyQueue);
Message replyMsg = consumer.receive(5000);
Assert.assertNotNull(replyMsg);
Assert.assertTrue(replyMsg instanceof TextMessage);
String actual = ((TextMessage) replyMsg).getText();
Assert.assertEquals("Howdy Fred! GoodBye user1", actual);
} finally {
if (session != null) {
session.close();
}
closeConnection(connection);
}
}
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 camel by apache.
the class InOutMessageHandler method handleMessage.
@Override
public void handleMessage(final Exchange exchange) {
try {
MessageProducer messageProducer = null;
Object obj = exchange.getIn().getHeader(JmsConstants.JMS_REPLY_TO);
if (obj != null) {
Destination replyTo;
if (isDestination(obj)) {
replyTo = (Destination) obj;
} else if (obj instanceof String) {
replyTo = getEndpoint().getDestinationCreationStrategy().createDestination(getSession(), (String) obj, isTopic());
} else {
throw new Exception("The value of JMSReplyTo must be a valid Destination or String. Value provided: " + obj);
}
String destinationName = getDestinationName(replyTo);
try {
lock.readLock().lock();
if (producerCache.containsKey(destinationName)) {
messageProducer = producerCache.get(destinationName);
}
} finally {
lock.readLock().unlock();
}
if (messageProducer == null) {
try {
lock.writeLock().lock();
messageProducer = getSession().createProducer(replyTo);
producerCache.put(destinationName, messageProducer);
} finally {
lock.writeLock().unlock();
}
}
}
MessageHandlerAsyncCallback callback = new MessageHandlerAsyncCallback(exchange, messageProducer);
if (exchange.isFailed()) {
return;
} else {
if (isTransacted() || isSynchronous()) {
// must process synchronous if transacted or configured to
// do so
log.debug("Synchronous processing: Message[{}], Destination[{}] ", exchange.getIn().getBody(), getEndpoint().getEndpointUri());
try {
getProcessor().process(exchange);
} catch (Exception e) {
exchange.setException(e);
} finally {
callback.done(true);
}
} else {
// process asynchronous using the async routing engine
log.debug("Asynchronous processing: Message[{}], Destination[{}] ", exchange.getIn().getBody(), getEndpoint().getEndpointUri());
getProcessor().process(exchange, callback);
}
}
} catch (Exception e) {
exchange.setException(e);
}
if (log.isDebugEnabled()) {
log.debug("SjmsMessageConsumer invoked for Exchange id:{} ", exchange.getExchangeId());
}
}
Aggregations