use of javax.jms.Connection in project wildfly by wildfly.
the class TransactedTopicMessageSender method sendToTopicSuccessfully.
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void sendToTopicSuccessfully() 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(topic);
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();
}
}
}
use of javax.jms.Connection in project wildfly by wildfly.
the class JmsClientTestCase method doSendAndReceive.
private void doSendAndReceive(String connectionFactoryLookup) throws Exception {
Connection conn = null;
try {
ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup(connectionFactoryLookup);
assertNotNull(cf);
Destination destination = (Destination) remoteContext.lookup(QUEUE_NAME);
assertNotNull(destination);
conn = cf.createConnection("guest", "guest");
conn.start();
Session consumerSession = conn.createSession(false, AUTO_ACKNOWLEDGE);
final CountDownLatch latch = new CountDownLatch(10);
final List<String> result = new ArrayList<String>();
// Set the async listener
MessageConsumer consumer = consumerSession.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage msg = (TextMessage) message;
try {
result.add(msg.getText());
latch.countDown();
} catch (JMSException e) {
e.printStackTrace();
}
}
});
final Session producerSession = conn.createSession(false, AUTO_ACKNOWLEDGE);
MessageProducer producer = producerSession.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i = 0; i < 10; i++) {
String s = "Test" + i;
TextMessage msg = producerSession.createTextMessage(s);
//System.out.println("sending " + s);
producer.send(msg);
}
producerSession.close();
assertTrue(latch.await(3, SECONDS));
assertEquals(10, result.size());
for (int i = 0; i < result.size(); i++) {
assertEquals("Test" + i, result.get(i));
}
} finally {
try {
conn.close();
} catch (Exception ignore) {
}
}
}
use of javax.jms.Connection in project tomee by apache.
the class AMQXASupportTest method xaCode.
@Test
public void xaCode() throws Exception {
assertNotNull(xacf);
final Connection connection = xacf.createXAConnection();
assertThat(connection, instanceOf(XAConnection.class));
testConnection(connection);
}
use of javax.jms.Connection in project tomee by apache.
the class AMQXASupportTest method standardCode.
@Test
public void standardCode() throws Exception {
assertNotNull(cf);
final Connection connection = cf.createConnection();
testConnection(connection);
}
use of javax.jms.Connection in project sling by apache.
the class JMSQueueManagerTest method emptyQueue.
private void emptyQueue(String name) throws JMSException {
dumpQueue(name);
Connection connection = amqConnectionFactoryService.getConnectionFactory().createConnection();
connection.start();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Queue queue = session.createQueue(name);
MessageConsumer consumer = session.createConsumer(queue);
for (; ; ) {
Message m = consumer.receive(100);
if (m == null) {
LOGGER.info("No more messages in queue {} ", name);
break;
}
LOGGER.info("Got message {}", m);
m.acknowledge();
session.commit();
}
boolean shouldFail = false;
QueueBrowser browser = session.createBrowser(queue);
for (Enumeration messages = browser.getEnumeration(); messages.hasMoreElements(); ) {
Message m = (Message) messages.nextElement();
LOGGER.info("Queued message {} ", m);
shouldFail = true;
}
browser.close();
if (shouldFail) {
fail("Queue was not emptied as expected");
}
consumer.close();
session.close();
connection.stop();
}
Aggregations