use of javax.jms.JMSProducer in project qpid-broker-j by apache.
the class DeliveryDelayTest method testDeliveryDelay.
@Test
public void testDeliveryDelay() throws Exception {
try (JMSContext context = getConnectionBuilder().buildConnectionFactory().createContext()) {
Destination queue = createQueue(context, BrokerAdmin.TEST_QUEUE_NAME, true);
final AtomicLong messageReceiptTime = new AtomicLong();
final CountDownLatch receivedLatch = new CountDownLatch(1);
context.createConsumer(queue).setMessageListener(message -> {
messageReceiptTime.set(System.currentTimeMillis());
receivedLatch.countDown();
});
JMSProducer producer = context.createProducer().setDeliveryDelay(DELIVERY_DELAY);
final long messageSentTime = System.currentTimeMillis();
producer.send(queue, "delayed message");
final boolean messageArrived = receivedLatch.await(DELIVERY_DELAY * 3, TimeUnit.MILLISECONDS);
assertTrue("Delayed message did not arrive within expected period", messageArrived);
final long actualDelay = messageReceiptTime.get() - messageSentTime;
assertTrue(String.format("Message was not delayed by sufficient time (%d). Actual delay (%d)", DELIVERY_DELAY, actualDelay), actualDelay >= DELIVERY_DELAY);
}
}
use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class OutgoingConnectionJTATest method testSimpleSendNoXAJMSContext.
@Test
public void testSimpleSendNoXAJMSContext() throws Exception {
Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
try (ClientSessionFactory sf = locator.createSessionFactory();
ClientSession session = sf.createSession();
ClientConsumer consVerify = session.createConsumer(MDBQUEUE);
JMSContext jmsctx = qraConnectionFactory.createContext()) {
session.start();
// These next 4 lines could be written in a single line however it makes difficult for debugging
JMSProducer producer = jmsctx.createProducer();
producer.setProperty("strvalue", "hello");
TextMessage msgsend = jmsctx.createTextMessage("hello");
producer.send(q, msgsend);
ClientMessage msg = consVerify.receive(1000);
assertNotNull(msg);
assertEquals("hello", msg.getStringProperty("strvalue"));
}
}
use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class JMSAutoCloseableExample method main.
public static void main(final String[] args) throws Exception {
// Step 2. Perfom a lookup on the queue
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Step 4.Create a JMS Context using the try-with-resources statement
try (// Even though ConnectionFactory is not closeable it would be nice to close an ActiveMQConnectionFactory
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
JMSContext jmsContext = cf.createContext()) {
// Step 5. create a jms producer
JMSProducer jmsProducer = jmsContext.createProducer();
// Step 6. Try sending a message, we don't have the appropriate privileges to do this so this will throw an exception
jmsProducer.send(queue, "A Message from JMS2!");
System.out.println("Received:" + jmsContext.createConsumer(queue).receiveBody(String.class));
}
}
use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class JmsConsumerTest method testIndividualACKJms2.
@Test
public void testIndividualACKJms2() throws Exception {
JMSContext context = cf.createContext(ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
JMSProducer producer = context.createProducer();
JMSConsumer consumer = context.createConsumer(jBossQueue);
int noOfMessages = 100;
for (int i = 0; i < noOfMessages; i++) {
producer.send(jBossQueue, context.createTextMessage("m" + i));
}
context.start();
// Consume even numbers first
for (int i = 0; i < noOfMessages; i++) {
Message m = consumer.receive(500);
Assert.assertNotNull(m);
if (i % 2 == 0) {
m.acknowledge();
}
}
context.close();
context = cf.createContext(ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
consumer = context.createConsumer(jBossQueue);
// Consume odd numbers first
for (int i = 0; i < noOfMessages; i++) {
if (i % 2 == 0) {
continue;
}
TextMessage m = (TextMessage) consumer.receive(1000);
Assert.assertNotNull(m);
m.acknowledge();
Assert.assertEquals("m" + i, m.getText());
}
SimpleString queueName = new SimpleString(JmsConsumerTest.Q_NAME);
context.close();
Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(queueName).getBindable()).getDeliveringCount());
Assert.assertEquals(0, getMessageCount((Queue) server.getPostOffice().getBinding(queueName).getBindable()));
}
use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class InvalidDestinationTest method invalidDestinationRuntimeExceptionTests.
@Test
public void invalidDestinationRuntimeExceptionTests() throws Exception {
JMSProducer producer = context.createProducer();
Destination invalidDestination = null;
Topic invalidTopic = null;
String message = "hello world";
byte[] bytesMsgSend = message.getBytes();
Map<String, Object> mapMsgSend = new HashMap<>();
mapMsgSend.put("s", "foo");
mapMsgSend.put("b", true);
mapMsgSend.put("i", 1);
TextMessage expTextMessage = context.createTextMessage(message);
try {
producer.send(invalidDestination, expTextMessage);
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
producer.send(invalidDestination, message);
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
ObjectMessage om = context.createObjectMessage();
StringBuffer sb = new StringBuffer(message);
om.setObject(sb);
try {
producer.send(invalidDestination, om);
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
producer.send(invalidDestination, bytesMsgSend);
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
producer.send(invalidDestination, mapMsgSend);
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
context.createConsumer(invalidDestination);
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
context.createConsumer(invalidDestination, "lastMessage = TRUE");
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
context.createConsumer(invalidDestination, "lastMessage = TRUE", false);
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
context.createDurableConsumer(invalidTopic, "InvalidDestinationRuntimeException");
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
context.createDurableConsumer(invalidTopic, "InvalidDestinationRuntimeException", "lastMessage = TRUE", false);
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
context.createSharedDurableConsumer(invalidTopic, "InvalidDestinationRuntimeException");
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
context.createSharedDurableConsumer(invalidTopic, "InvalidDestinationRuntimeException", "lastMessage = TRUE");
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
context.unsubscribe("InvalidSubscriptionName");
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
context.createSharedConsumer(invalidTopic, "InvalidDestinationRuntimeException");
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
try {
context.createSharedConsumer(invalidTopic, "InvalidDestinationRuntimeException", "lastMessage = TRUE");
} catch (InvalidDestinationRuntimeException e) {
// pass
} catch (Exception e) {
fail("Expected InvalidDestinationRuntimeException, received " + e);
}
}
Aggregations