use of javax.jms.TextMessage in project wildfly by wildfly.
the class LegacyJMSTestCase method doSendAndReceive.
private void doSendAndReceive(String connectionFactoryLookup, String destinationLoookup) throws Exception {
Destination destination = (Destination) remoteContext.lookup(destinationLoookup);
assertNotNull(destination);
ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup(connectionFactoryLookup);
assertNotNull(cf);
try (JMSContext producerContext = cf.createContext("guest", "guest");
JMSContext consumerContext = cf.createContext("guest", "guest")) {
final CountDownLatch latch = new CountDownLatch(10);
final List<String> result = new ArrayList<String>();
JMSConsumer consumer = consumerContext.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();
}
}
});
JMSProducer producer = producerContext.createProducer();
for (int i = 0; i < 10; i++) {
String text = "Test" + i;
producer.send(destination, text);
}
assertTrue(latch.await(3, SECONDS));
assertEquals(10, result.size());
for (int i = 0; i < result.size(); i++) {
assertEquals("Test" + i, result.get(i));
}
}
}
use of javax.jms.TextMessage 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.TextMessage 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.TextMessage 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.TextMessage 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) {
}
}
}
Aggregations