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 karaf by apache.
the class ArtemisDestinationSourceFactory method getNames.
private List<String> getNames(Connection connection, DestinationSource.DestinationType type) {
try {
QueueSession session = ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue managementQueue = session.createQueue("activemq.management");
QueueRequestor requestor = new QueueRequestor(session, managementQueue);
connection.start();
TextMessage m = session.createTextMessage();
m.setStringProperty("_AMQ_ResourceName", "broker");
m.setStringProperty("_AMQ_OperationName", "getQueueNames");
String routing = type == DestinationSource.DestinationType.Queue ? "ANYCAST" : "MULTICAST";
m.setText("[\"" + routing + "\"]");
Message reply = requestor.request(m);
String json = ((TextMessage) reply).getText();
List<?> array = (List<?>) JsonReader.read(new StringReader(json));
return (List<String>) array.get(0);
} catch (Exception e) {
return Collections.emptyList();
}
}
use of javax.jms.Message in project logging-log4j2 by apache.
the class JmsAppender method append.
@Override
public void append(final LogEvent event) {
try {
final Message message = this.manager.createMessage(getLayout().toSerializable(event));
message.setJMSTimestamp(event.getTimeMillis());
this.producer.send(message);
} catch (final JMSException e) {
throw new AppenderLoggingException(e);
}
}
use of javax.jms.Message in project jmeter by apache.
the class JMSSampler method sample.
/**
* {@inheritDoc}
*/
@Override
public SampleResult sample(Entry entry) {
SampleResult res = new SampleResult();
res.setSampleLabel(getName());
res.setSamplerData(getContent());
// Assume failure
res.setSuccessful(false);
res.setDataType(SampleResult.TEXT);
res.sampleStart();
try {
TextMessage msg = createMessage();
if (isOneway()) {
int deliveryMode = isNonPersistent() ? DeliveryMode.NON_PERSISTENT : DeliveryMode.PERSISTENT;
producer.send(msg, deliveryMode, Integer.parseInt(getPriority()), Long.parseLong(getExpiration()));
res.setRequestHeaders(Utils.messageProperties(msg));
res.setResponseOK();
res.setResponseData("Oneway request has no response data", null);
} else {
if (!useTemporyQueue()) {
msg.setJMSReplyTo(receiveQueue);
}
Message replyMsg = executor.sendAndReceive(msg, isNonPersistent() ? DeliveryMode.NON_PERSISTENT : DeliveryMode.PERSISTENT, Integer.parseInt(getPriority()), Long.parseLong(getExpiration()));
res.setRequestHeaders(Utils.messageProperties(msg));
if (replyMsg == null) {
res.setResponseMessage("No reply message received");
} else {
if (replyMsg instanceof TextMessage) {
res.setResponseData(((TextMessage) replyMsg).getText(), null);
} else {
res.setResponseData(replyMsg.toString(), null);
}
res.setResponseHeaders(Utils.messageProperties(replyMsg));
res.setResponseOK();
}
}
} catch (Exception e) {
LOGGER.warn(e.getLocalizedMessage(), e);
if (thrown != null) {
res.setResponseMessage(thrown.toString());
} else {
res.setResponseMessage(e.getLocalizedMessage());
}
}
res.sampleEnd();
return res;
}
Aggregations