use of javax.jms.MessageProducer in project cxf by apache.
the class PooledConnectionTempQueueTest method receiveAndRespondWithMessageIdAsCorrelationId.
public void receiveAndRespondWithMessageIdAsCorrelationId(ConnectionFactory connectionFactory, String queueName) throws JMSException {
Connection con = connectionFactory.createConnection();
Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(session.createQueue(queueName));
final javax.jms.Message inMessage = consumer.receive();
// String requestMessageId = inMessage.getJMSMessageID();
// System.out.println("Received message " + requestMessageId);
final TextMessage replyMessage = session.createTextMessage("Result");
replyMessage.setJMSCorrelationID(inMessage.getJMSMessageID());
final MessageProducer producer = session.createProducer(inMessage.getJMSReplyTo());
// System.out.println("Sending reply to " + inMessage.getJMSReplyTo());
producer.send(replyMessage);
producer.close();
consumer.close();
session.close();
con.close();
}
use of javax.jms.MessageProducer in project apex-malhar by apache.
the class JMSTestBase method produceMsg.
public void produceMsg(String text) throws Exception {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
TextMessage message = session.createTextMessage(text);
producer.send(message);
// Clean up
session.close();
connection.close();
}
use of javax.jms.MessageProducer in project tomee by apache.
the class JmsTest method createSender.
@SuppressWarnings("unchecked")
private synchronized void createSender(final Connection connection, final Destination requestQueue) throws JMSException {
Session session = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
try {
// create request
final Map<String, Object> request = new TreeMap<String, Object>();
request.put("args", new Object[] { "cheese" });
// create a new temp response queue
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination responseQueue = session.createTemporaryQueue();
// Create a request messages
final ObjectMessage requestMessage = session.createObjectMessage();
requestMessage.setJMSReplyTo(responseQueue);
requestMessage.setObject((Serializable) request);
// Send the request message
producer = session.createProducer(requestQueue);
producer.send(requestMessage);
// wait for the response message
consumer = session.createConsumer(responseQueue);
final Message message = consumer.receive(30000);
// verify message
assertNotNull("Did not get a response message", message);
assertTrue("Response message is not an ObjectMessage", message instanceof ObjectMessage);
final ObjectMessage responseMessage = (ObjectMessage) message;
final Serializable object = responseMessage.getObject();
assertNotNull("Response ObjectMessage contains a null object");
assertTrue("Response ObjectMessage does not contain an instance of Map", object instanceof Map);
final Map<String, String> response = (Map<String, String>) object;
// process results
final String returnValue = response.get("return");
assertEquals("test-cheese", returnValue);
} finally {
MdbUtil.close(consumer);
MdbUtil.close(producer);
MdbUtil.close(session);
}
}
use of javax.jms.MessageProducer in project tomee by apache.
the class MdbContainerTest method shouldSendMessage.
@Test
public void shouldSendMessage() throws Exception {
assertNotNull(cf);
final Connection connection = cf.createConnection();
try {
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final MessageProducer producer = session.createProducer(destination);
producer.send(session.createTextMessage(TEXT));
assertTrue(AMQXASupportTest.Listener.sync());
} finally {
connection.close();
}
}
use of javax.jms.MessageProducer in project tomee by apache.
the class MdbInvoker method onMessage.
public void onMessage(final Message message) {
if (!(message instanceof ObjectMessage))
return;
try {
final Session session = getSession();
if (session == null)
throw new IllegalStateException("Invoker has been destroyed");
if (message == null)
throw new NullPointerException("request message is null");
if (!(message instanceof ObjectMessage))
throw new IllegalArgumentException("Expected a ObjectMessage request but got a " + message.getClass().getName());
final ObjectMessage objectMessage = (ObjectMessage) message;
final Serializable object = objectMessage.getObject();
if (object == null)
throw new NullPointerException("object in ObjectMessage is null");
if (!(object instanceof Map)) {
if (message instanceof ObjectMessage)
throw new IllegalArgumentException("Expected a Map contained in the ObjectMessage request but got a " + object.getClass().getName());
}
final Map request = (Map) object;
final String signature = (String) request.get("method");
final Method method = signatures.get(signature);
final Object[] args = (Object[]) request.get("args");
boolean exception = false;
Object result = null;
try {
result = method.invoke(target, args);
} catch (final IllegalAccessException e) {
result = e;
exception = true;
} catch (final InvocationTargetException e) {
result = e.getCause();
if (result == null)
result = e;
exception = true;
}
MessageProducer producer = null;
try {
// create response
final Map<String, Object> response = new TreeMap<String, Object>();
if (exception) {
response.put("exception", "true");
}
response.put("return", result);
// create response message
final ObjectMessage resMessage = session.createObjectMessage();
resMessage.setJMSCorrelationID(objectMessage.getJMSCorrelationID());
resMessage.setObject((Serializable) response);
// send response message
producer = session.createProducer(objectMessage.getJMSReplyTo());
producer.send(resMessage);
} catch (final Exception e) {
e.printStackTrace();
} finally {
MdbUtil.close(producer);
destroy();
}
} catch (final Throwable e) {
e.printStackTrace();
}
}
Aggregations