use of javax.jms.Session in project quickstarts by jboss-switchyard.
the class JMSClient method sendToHornetQ.
private static void sendToHornetQ(String payload, String queueName) throws Exception {
HornetQMixIn hqMixIn = new HornetQMixIn(false).setUser(HQ_USER).setPassword(HQ_PASSWD);
hqMixIn.initialize();
try {
Session session = hqMixIn.getJMSSession();
MessageProducer producer = session.createProducer(HornetQMixIn.getJMSQueue(queueName));
producer.send(hqMixIn.createJMSMessage(payload));
session.close();
verifyOutputQueue(hqMixIn.createJMSSession());
} finally {
hqMixIn.uninitialize();
}
}
use of javax.jms.Session in project quickstarts by jboss-switchyard.
the class JmsBindingTest method testRollbackB.
@Test
public void testRollbackB() throws Exception {
String command = "rollback.B";
Connection conn = _connectionFactory.createConnection(HORNETQ_USER, HORNETQ_PASSWORD);
conn.start();
try {
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(_queueIn);
TextMessage message = session.createTextMessage();
message.setText(command);
producer.send(message);
session.close();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(_queueOutA);
TextMessage msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
Assert.assertNull(consumer.receive(1000));
consumer.close();
consumer = session.createConsumer(_queueOutB);
msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
Assert.assertNull(consumer.receive(1000));
consumer.close();
consumer = session.createConsumer(_queueOutC);
msg = TextMessage.class.cast(consumer.receive(1000));
Assert.assertEquals(command, msg.getText());
Assert.assertNull(consumer.receive(1000));
session.close();
} finally {
conn.close();
}
}
use of javax.jms.Session in project quickstarts by jboss-switchyard.
the class JmsBindingTest method testNonTransacted.
@Test
public void testNonTransacted() throws Exception {
String command = "rollback.A";
Connection conn = _connectionFactory.createConnection(HORNETQ_USER, HORNETQ_PASSWORD);
conn.start();
try {
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(_queueInNoTx);
TextMessage message = session.createTextMessage();
message.setText(command);
producer.send(message);
session.close();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(_queueOutA);
Assert.assertNull(consumer.receive(1000));
consumer = session.createConsumer(_queueOutB);
Assert.assertNull(consumer.receive(1000));
consumer = session.createConsumer(_queueOutC);
Assert.assertNull(consumer.receive(1000));
} finally {
conn.close();
}
}
use of javax.jms.Session in project quickstarts by jboss-switchyard.
the class ActiveMQClient method main.
/**
* Only execution point for this application.
* @param ignored not used.
* @throws Exception if something goes wrong.
*/
public static void main(final String[] args) throws Exception {
String[] orders = { "BREAD", "PIZZA", "JAM", "POTATO", "MILK", "JAM" };
if (args.length != 0) {
orders = args;
}
ActiveMQMixIn mixIn = new ActiveMQMixIn();
try {
Session session = mixIn.getSession();
MessageProducer producer = session.createProducer(session.createQueue(ORDER_QUEUE));
for (String order : orders) {
final TextMessage message = session.createTextMessage();
message.setText(order);
producer.send(message);
}
session.close();
session = mixIn.getSession();
System.out.println("* * * SHIPPING ORDERS * * *");
MessageConsumer consumer = session.createConsumer(session.createQueue(SHIPPING_QUEUE));
Message msg = null;
while ((msg = consumer.receive(1000)) != null) {
if (msg instanceof TextMessage) {
System.out.println(" - " + ((TextMessage) msg).getText());
}
}
System.out.println();
System.out.println("* * * PENDING ORDERS (FILLING STOCK) * * *");
consumer = session.createConsumer(session.createQueue(FILLING_STOCK_QUEUE));
while ((msg = consumer.receive(1000)) != null) {
if (msg instanceof TextMessage) {
System.out.println(" - " + ((TextMessage) msg).getText());
}
}
session.close();
Thread.sleep(2000);
} finally {
mixIn.uninitialize();
}
}
use of javax.jms.Session in project spring-framework by spring-projects.
the class AbstractMessageListenerContainer method doInvokeListener.
/**
* Invoke the specified listener as Spring SessionAwareMessageListener,
* exposing a new JMS Session (potentially with its own transaction)
* to the listener if demanded.
* @param listener the Spring SessionAwareMessageListener to invoke
* @param session the JMS Session to operate on
* @param message the received JMS Message
* @throws JMSException if thrown by JMS API methods
* @see SessionAwareMessageListener
* @see #setExposeListenerSession
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void doInvokeListener(SessionAwareMessageListener listener, Session session, Message message) throws JMSException {
Connection conToClose = null;
Session sessionToClose = null;
try {
Session sessionToUse = session;
if (!isExposeListenerSession()) {
// We need to expose a separate Session.
conToClose = createConnection();
sessionToClose = createSession(conToClose);
sessionToUse = sessionToClose;
}
// Actually invoke the message listener...
listener.onMessage(message, sessionToUse);
// Clean up specially exposed Session, if any.
if (sessionToUse != session) {
if (sessionToUse.getTransacted() && isSessionLocallyTransacted(sessionToUse)) {
// Transacted session created by this container -> commit.
JmsUtils.commitIfNecessary(sessionToUse);
}
}
} finally {
JmsUtils.closeSession(sessionToClose);
JmsUtils.closeConnection(conToClose);
}
}
Aggregations