use of javax.jms.MessageConsumer 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.MessageConsumer in project adempiere by adempiere.
the class TopicListener method run.
public void run() throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
log.finest("ActiveMQConnectionFactory = " + factory);
if (userName != null && password != null) {
conn = factory.createConnection(userName, password);
} else {
conn = factory.createConnection();
}
log.finest("conn = " + conn);
if (conn.getClientID() == null) {
try {
conn.setClientID(clientID);
} catch (Exception e) {
//log.info("Connection with clientID '" + clientID +"' already exists" + e.toString());
conn.close();
return;
}
} else {
if (conn.getClientID().equals(clientID)) {
log.warning("Connection with clientID '" + clientID + "' already exists");
conn.close();
return;
} else {
try {
conn.setClientID(clientID);
} catch (Exception e) {
log.info("Error while invoking setClientID(" + clientID + ")! " + e.getMessage());
conn.close();
return;
}
}
}
// TODO - could be parameter
session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
log.finest("session = " + session);
log.finest("topicName = " + topicName);
log.finest("subscriptionName = " + subscriptionName);
topic = session.createTopic(topicName);
log.finest("topic = " + topic);
MessageConsumer consumer = null;
if (isDurableSubscription) {
// http://publib.boulder.ibm.com/infocenter/wasinfo/v6r0/index.jsp?topic=/com.ibm.websphere.pmc.express.doc/tasks/tjn0012_.html
// The subscriptionName assigned to a durable subscription must be unique within a given client ID.
consumer = session.createDurableSubscriber(topic, subscriptionName);
} else {
consumer = session.createConsumer(topic);
}
log.finest("consumer = " + consumer);
consumer.setMessageListener(this);
conn.start();
log.finest("Waiting for JMS messages...");
if (replicationProcessor != null) {
MIMPProcessorLog pLog = new MIMPProcessorLog(replicationProcessor.getMImportProcessor(), "Connected to JMS Server. Waiting for messages!");
StringBuffer logReference = new StringBuffer("topicName = ").append(topicName).append(", subscriptionName = ").append(subscriptionName);
pLog.setReference(logReference.toString());
boolean resultSave = pLog.save();
log.finest("Result Save = " + resultSave);
}
}
use of javax.jms.MessageConsumer in project ignite by apache.
the class JmsStreamer method initializeJmsObjectsForQueue.
private void initializeJmsObjectsForQueue() throws JMSException {
for (int i = 0; i < threads; i++) {
Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
if (destination == null)
destination = session.createQueue(destinationName);
MessageConsumer consumer = session.createConsumer(destination);
IgniteJmsMessageListener messageListener = new IgniteJmsMessageListener(session, false);
consumer.setMessageListener(messageListener);
consumers.add(consumer);
sessions.add(session);
listeners.add(messageListener);
}
}
use of javax.jms.MessageConsumer in project logging-log4j2 by apache.
the class JmsAppenderIT method testLogMapMessageToQueue.
@Test
public void testLogMapMessageToQueue() throws Exception {
setUp(MessageLayout.createLayout());
final int messageCount = 100;
final MessageConsumer messageConsumer = jmsManager.createMessageConsumer();
final JmsQueueConsumer consumer = new JmsQueueConsumer(messageCount, javax.jms.MapMessage.class);
messageConsumer.setMessageListener(consumer);
final String messageText = "Hello, World!";
final String loggerName = this.getClass().getName();
for (int i = 0; i < messageCount; i++) {
Map<String, String> map = new HashMap<>();
map.put("messageText", messageText);
map.put("threadName", Thread.currentThread().getName());
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(loggerName).setLoggerFqcn(loggerName).setLevel(//
Level.INFO).setMessage(//
new MapMessage(map)).setTimeMillis(System.currentTimeMillis()).build();
appender.append(event);
}
consumer.awaitAndAssertAllMessagesConsumed();
}
use of javax.jms.MessageConsumer in project logging-log4j2 by apache.
the class JmsAppenderIT method testLogObjectMessageToQueue.
@Test
public void testLogObjectMessageToQueue() throws Exception {
setUp(SerializedLayout.createLayout());
final int messageCount = 100;
final MessageConsumer messageConsumer = jmsManager.createMessageConsumer();
final JmsQueueConsumer consumer = new JmsQueueConsumer(messageCount, ObjectMessage.class);
messageConsumer.setMessageListener(consumer);
final String messageText = "Hello, World!";
final String loggerName = this.getClass().getName();
for (int i = 0; i < messageCount; i++) {
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(loggerName).setLoggerFqcn(loggerName).setLevel(//
Level.INFO).setMessage(new SimpleMessage(messageText)).setThreadName(//
Thread.currentThread().getName()).setTimeMillis(System.currentTimeMillis()).build();
appender.append(event);
}
consumer.awaitAndAssertAllMessagesConsumed();
}
Aggregations