use of javax.jms.TextMessage in project quickstarts by jboss-switchyard.
the class ActiveMQClient method main.
/**
* Only execution point for this application.
* @param ignored not used.
* @throws Exception if something goes wrong.
*/
public static void main(final String[] args) throws Exception {
String[] orders = { "BREAD", "PIZZA", "JAM", "POTATO", "MILK", "JAM" };
if (args.length != 0) {
orders = args;
}
ActiveMQMixIn mixIn = new ActiveMQMixIn();
try {
Session session = mixIn.getSession();
MessageProducer producer = session.createProducer(session.createQueue(ORDER_QUEUE));
for (String order : orders) {
final TextMessage message = session.createTextMessage();
message.setText(order);
producer.send(message);
}
session.close();
session = mixIn.getSession();
System.out.println("* * * SHIPPING ORDERS * * *");
MessageConsumer consumer = session.createConsumer(session.createQueue(SHIPPING_QUEUE));
Message msg = null;
while ((msg = consumer.receive(1000)) != null) {
if (msg instanceof TextMessage) {
System.out.println(" - " + ((TextMessage) msg).getText());
}
}
System.out.println();
System.out.println("* * * PENDING ORDERS (FILLING STOCK) * * *");
consumer = session.createConsumer(session.createQueue(FILLING_STOCK_QUEUE));
while ((msg = consumer.receive(1000)) != null) {
if (msg instanceof TextMessage) {
System.out.println(" - " + ((TextMessage) msg).getText());
}
}
session.close();
Thread.sleep(2000);
} finally {
mixIn.uninitialize();
}
}
use of javax.jms.TextMessage in project pinpoint by naver.
the class ActiveMQClientITBase method testQueuePush.
@Test
public void testQueuePush() throws Exception {
// Given
final String testQueueName = "TestPushQueue";
final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName);
final String testMessage = "Hello World for Queue!";
final CountDownLatch consumerLatch = new CountDownLatch(1);
final Collection<Throwable> consumerThrowables = new CopyOnWriteArrayList<Throwable>();
// create producer
ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
MessageProducer producer = producerSession.createProducer(testQueue);
final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
// create consumer
ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
MessageConsumer consumer = consumerSession.createConsumer(testQueue);
consumer.setMessageListener(new AssertTextMessageListener(consumerLatch, consumerThrowables, expectedTextMessage));
// When
producer.send(expectedTextMessage);
consumerLatch.await(1L, TimeUnit.SECONDS);
// Then
assertNoConsumerError(consumerThrowables);
// Wait till all traces are recorded (consumer traces are recorded from another thread)
awaitAndVerifyTraceCount(2, 5000L);
// trace count : 1
verifyProducerSendEvent(testQueue);
// trace count : 1
verifyConsumerPushEvent(testQueue);
}
use of javax.jms.TextMessage in project pinpoint by naver.
the class ActiveMQClientITBase method testTopicPush.
@Test
public void testTopicPush() throws Exception {
// Given
final String testTopicName = "TestPushTopic";
final ActiveMQTopic testTopic = new ActiveMQTopic(testTopicName);
final String testMessage = "Hello World for Topic!";
final int numMessageConsumers = 2;
final CountDownLatch consumerConsumeLatch = new CountDownLatch(numMessageConsumers);
final Collection<Throwable> consumerThrowables = new CopyOnWriteArrayList<Throwable>();
// create producer
ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
MessageProducer producer = new MessageProducerBuilder(producerSession, testTopic).waitTillStarted().build();
final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
// create 2 consumers
ActiveMQSession consumer1Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
new MessageConsumerBuilder(consumer1Session, testTopic).withMessageListener(new AssertTextMessageListener(consumerConsumeLatch, consumerThrowables, expectedTextMessage)).waitTillStarted().build();
ActiveMQSession consumer2Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
new MessageConsumerBuilder(consumer2Session, testTopic).withMessageListener(new AssertTextMessageListener(consumerConsumeLatch, consumerThrowables, expectedTextMessage)).waitTillStarted().build();
// When
producer.send(expectedTextMessage);
consumerConsumeLatch.await(1L, TimeUnit.SECONDS);
// Then
// Wait till all traces are recorded (consumer traces are recorded from another thread)
awaitAndVerifyTraceCount(3, 1000L);
// trace count : 1
verifyProducerSendEvent(testTopic);
// trace count : 1
verifyConsumerPushEvent(testTopic);
// trace count : 1
verifyConsumerPushEvent(testTopic);
}
use of javax.jms.TextMessage in project pinpoint by naver.
the class ActiveMQClientITBase method testQueuePull.
@Test
public void testQueuePull() throws Exception {
// Given
final String testQueueName = "TestPullQueue";
final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName);
final String testMessage = "Hello World for Queue!";
// create producer
ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
MessageProducer producer = producerSession.createProducer(testQueue);
final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
// When
ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
MessageConsumer consumer = consumerSession.createConsumer(testQueue);
// Then
producer.send(expectedTextMessage);
Message message = consumer.receive(1000L);
Assert.assertEquals(testMessage, ((TextMessage) message).getText());
// Wait till all traces are recorded (consumer traces are recorded from another thread)
awaitAndVerifyTraceCount(5, 5000L);
// trace count : 1
verifyProducerSendEvent(testQueue);
// trace count : 4
verifyConsumerPullEvent(testQueue, consumer, expectedTextMessage);
}
use of javax.jms.TextMessage in project meteo by pierre.
the class TopicListener method onMessage.
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String txt = null;
try {
txt = txtMsg.getText();
log.debug("Received a message, yay!\n" + txt);
Map event = mapper.readValue(txt, Map.class);
esperSink.getEPRuntime().sendEvent(event, esperTopicKey);
} catch (JMSException ex) {
log.warn("Got an error from the message queue", ex);
} catch (ClassCastException ex) {
log.info("Received message that I couldn't parse: " + txt, ex);
} catch (JsonMappingException ex) {
log.info("Received message that I couldn't parse: " + txt, ex);
} catch (JsonParseException ex) {
log.info("Received message that I couldn't parse: " + txt, ex);
} catch (IOException ex) {
log.warn("Got an error from the message queue", ex);
}
} else if (message instanceof BytesMessage) {
final BytesMessage byteMessage = (BytesMessage) message;
long llen;
try {
llen = byteMessage.getBodyLength();
} catch (JMSException e) {
log.warn("Unable to get message length", e);
return;
}
if (llen > Integer.MAX_VALUE) {
// should never occur but...
log.error("Ridiculously huge message payload, above 32-bit length");
} else {
final int len = (int) llen;
final byte[] data = new byte[len];
final int readLen;
try {
readLen = byteMessage.readBytes(data);
} catch (JMSException e) {
log.warn("Unable to get message bytes", e);
return;
}
if (readLen < len) {
log.error("Failed to read byte message contents; read {}, was trying to read {}", readLen, data.length);
} else {
final Map event;
try {
event = mapper.readValue(data, Map.class);
esperSink.getEPRuntime().sendEvent(event, esperTopicKey);
} catch (IOException e) {
log.error("Failed to convert message to Esper Event", readLen, data.length);
}
}
}
} else {
log.error("Unexpected message type '{}' from AMQ broker: must skip", message.getClass().getName());
}
}
Aggregations