use of javax.jms.Connection in project wildfly by wildfly.
the class DefaultJMSConnectionFactoryTest method assertMessageInQueue.
private void assertMessageInQueue(String text) throws JMSException {
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
connection.start();
Message message = consumer.receive(2000);
assertNotNull(message);
assertTrue(message instanceof TextMessage);
assertEquals(text, ((TextMessage) message).getText());
connection.close();
}
use of javax.jms.Connection in project wildfly by wildfly.
the class JMSBridgeTest method sendAndReceiveMessage.
/**
* Send a message on the source queue
* Consumes it on the target queue
*
* The test will pass since a JMS Bridge has been created to bridge the source destination to the target destination.
*/
@Test
public void sendAndReceiveMessage() throws Exception {
Connection connection = null;
Session session = null;
Message receivedMessage = null;
try {
// SEND A MESSAGE on the source queue
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(sourceQueue);
String text = MESSAGE_TEXT + " " + UUID.randomUUID().toString();
Message message = session.createTextMessage(text);
message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(message);
connection.start();
connection.close();
// RECEIVE THE MESSAGE from the target queue
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(targetQueue);
connection.start();
receivedMessage = consumer.receive(5000);
// ASSERTIONS
assertNotNull("did not receive expected message", receivedMessage);
assertTrue(receivedMessage instanceof TextMessage);
assertEquals(text, ((TextMessage) receivedMessage).getText());
assertNotNull("did not get header set by the JMS bridge", receivedMessage.getStringProperty(ActiveMQJMSConstants.AMQ_MESSAGING_BRIDGE_MESSAGE_ID_LIST));
} catch (Exception e) {
e.printStackTrace();
} finally {
// CLEANUP
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
}
use of javax.jms.Connection in project wildfly by wildfly.
the class SendToJMSTopicTest method sendMessage.
@Test
public void sendMessage() throws Exception {
Connection senderConnection = null;
Connection consumerConnection = null;
Session senderSession = null;
Session consumerSession = null;
MessageConsumer consumer = null;
try {
// CREATE SUBSCRIBER
logger.trace("******* Creating connection for consumer");
consumerConnection = factory.createConnection();
logger.trace("Creating session for consumer");
consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
logger.trace("Creating consumer");
consumer = consumerSession.createConsumer(topic);
logger.trace("Start session");
consumerConnection.start();
// SEND A MESSAGE
logger.trace("***** Start - sending message to topic");
senderConnection = factory.createConnection();
logger.trace("Creating session..");
senderSession = senderConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = senderSession.createProducer(topic);
TextMessage message = senderSession.createTextMessage("Hello world!");
logger.trace("Sending..");
producer.send(message);
logger.trace("Message sent");
senderConnection.start();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
logger.trace("Closing connections and sessions");
if (senderSession != null) {
senderSession.close();
}
if (senderConnection != null) {
senderConnection.close();
}
}
Message receivedMessage = null;
try {
logger.trace("Receiving");
receivedMessage = consumer.receive(5000);
logger.trace("Received: " + ((TextMessage) receivedMessage).getText());
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
} finally {
if (receivedMessage == null) {
Assert.fail("received null instead of a TextMessage");
}
if (consumerSession != null) {
consumerSession.close();
}
if (consumerConnection != null) {
consumerConnection.close();
}
}
Assert.assertTrue("received a " + receivedMessage.getClass().getName() + " instead of a TextMessage", receivedMessage instanceof TextMessage);
Assert.assertEquals(((TextMessage) receivedMessage).getText(), "Hello world!");
}
use of javax.jms.Connection in project wildfly by wildfly.
the class SimplifiedMessageProducer method send.
private void send(ConnectionFactory cf, Destination destination, String text) throws Exception {
// TODO use JMS 2.0 context when HornetQ supports it
Connection connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
Message message = session.createTextMessage(text);
message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(message);
connection.close();
}
use of javax.jms.Connection in project wildfly by wildfly.
the class TransactedQueueMessageSender method sendToQueueSuccessfully.
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void sendToQueueSuccessfully() throws Exception {
Connection connection = null;
Session session = null;
try {
logger.trace("Creating a Connection");
connection = factory.createConnection();
logger.trace("Creating a Session");
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
Message message = session.createTextMessage("Hello world!");
logger.trace("Sending message");
producer.send(message);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
}
Aggregations