use of javax.jms.Message 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.Message 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.Message in project tomee by apache.
the class JMS2AMQTest method receiveGetBody.
@Test
public void receiveGetBody() throws InterruptedException {
final String text = TEXT + "2";
final AtomicReference<Throwable> error = new AtomicReference<>();
final CountDownLatch ready = new CountDownLatch(1);
final CountDownLatch over = new CountDownLatch(1);
new Thread() {
@Override
public void run() {
{
setName(JMS2AMQTest.class.getName() + ".receiveGetBody#receiver");
}
try (final JMSContext context = cf.createContext()) {
try (final JMSConsumer consumer = context.createConsumer(destination2)) {
ready.countDown();
final Message receive = consumer.receive(TimeUnit.MINUTES.toMillis(1));
assertEquals(text, receive.getBody(String.class));
}
} catch (final Throwable ex) {
error.set(ex);
} finally {
over.countDown();
}
}
}.start();
ready.await(1, TimeUnit.MINUTES);
// just to ensure we called receive already
sleep(150);
// now send the message
try (final JMSContext context = cf.createContext()) {
context.createProducer().send(destination2, text);
} catch (final JMSRuntimeException ex) {
fail(ex.getMessage());
}
over.await(1, TimeUnit.MINUTES);
// ensure we got the message and no exception
final Throwable exception = error.get();
if (exception != null) {
exception.printStackTrace();
}
assertNull(exception == null ? "ok" : exception.getMessage(), exception);
}
use of javax.jms.Message in project tomee by apache.
the class JMSConsumerImpl method receiveBodyNoWait.
@Override
public <T> T receiveBodyNoWait(final Class<T> c) {
try {
final Message message = wrap(consumer.receiveNoWait());
context.setLastMessage(message);
return message == null ? null : message.getBody(c);
} catch (JMSException e) {
throw toRuntimeException(e);
}
}
use of javax.jms.Message in project tomee by apache.
the class JMSConsumerImpl method receiveBody.
@Override
public <T> T receiveBody(final Class<T> c, final long timeout) {
try {
final Message message = wrap(consumer.receive(timeout));
context.setLastMessage(message);
return message == null ? null : message.getBody(c);
} catch (final JMSException e) {
throw toRuntimeException(e);
}
}
Aggregations