use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class NonExistentQueueTest method sendToNonExistentDestination.
@Test
public void sendToNonExistentDestination() throws Exception {
server.getAddressSettingsRepository().addMatch("#", new AddressSettings().setAutoCreateQueues(false));
server.getAddressSettingsRepository().addMatch("#", new AddressSettings().setAutoCreateAddresses(false));
Destination destination = ActiveMQJMSClient.createTopic("DoesNotExist");
TransportConfiguration transportConfiguration = new TransportConfiguration(InVMConnectorFactory.class.getName());
ConnectionFactory localConnectionFactory = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, transportConfiguration);
// Using JMS 1 API
Connection connection = localConnectionFactory.createConnection();
Session session = connection.createSession();
try {
MessageProducer messageProducer = session.createProducer(null);
messageProducer.send(destination, session.createMessage());
Assert.fail("Succeeded in sending message to a non-existent destination using JMS 1 API!");
} catch (JMSException e) {
// Expected }
}
// Using JMS 2 API
JMSContext context = localConnectionFactory.createContext();
JMSProducer jmsProducer = context.createProducer().setDeliveryMode(DeliveryMode.PERSISTENT);
try {
jmsProducer.send(destination, context.createMessage());
Assert.fail("Succeeded in sending message to a non-existent destination using JMS 2 API!");
} catch (JMSRuntimeException e) {
// Expected }
}
}
use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class GroupingTest method testGroupingWithJMS2Producer.
@Test
public void testGroupingWithJMS2Producer() throws Exception {
ConnectionFactory fact = getCF();
Assume.assumeFalse("only makes sense withOUT auto-group", ((ActiveMQConnectionFactory) fact).isAutoGroup());
Assume.assumeTrue("only makes sense withOUT explicit group-id", ((ActiveMQConnectionFactory) fact).getGroupID() == null);
final String groupID = UUID.randomUUID().toString();
JMSContext ctx = addContext(getCF().createContext(JMSContext.SESSION_TRANSACTED));
JMSProducer producer = ctx.createProducer().setProperty("JMSXGroupID", groupID);
JMSConsumer consumer1 = ctx.createConsumer(queue);
JMSConsumer consumer2 = ctx.createConsumer(queue);
JMSConsumer consumer3 = ctx.createConsumer(queue);
ctx.start();
for (int j = 0; j < 100; j++) {
TextMessage message = ctx.createTextMessage("Message" + j);
producer.send(queue, message);
String prop = message.getStringProperty("JMSXGroupID");
assertNotNull(prop);
assertEquals(groupID, prop);
}
ctx.commit();
// All msgs should go to the first consumer
for (int j = 0; j < 100; j++) {
TextMessage tm = (TextMessage) consumer1.receive(10000);
assertNotNull(tm);
tm.acknowledge();
assertEquals("Message" + j, tm.getText());
assertEquals(tm.getStringProperty("JMSXGroupID"), groupID);
tm = (TextMessage) consumer2.receiveNoWait();
assertNull(tm);
tm = (TextMessage) consumer3.receiveNoWait();
assertNull(tm);
}
ctx.commit();
ctx.close();
}
use of javax.jms.JMSProducer in project eap-additional-testsuite by jboss-set.
the class AppScopedBean method sendMessage.
public void sendMessage() {
JMSProducer producer = context.createProducer();
producer.send(queue, "a message");
}
use of javax.jms.JMSProducer in project javaee7-samples by javaee-samples.
the class MessageSenderAsync method sendMessage.
/**
* Send a message to the JMS queue. Prin
*
* @param message the contents of the message.
* @throws JMSRuntimeException if an error occurs in accessing the queue.
*/
public void sendMessage(String message) throws JMSRuntimeException {
JMSProducer producer = context.createProducer();
try {
producer.setAsync(new CompletionListener() {
@Override
public void onCompletion(Message msg) {
try {
System.out.println(msg.getBody(String.class));
} catch (JMSException ex) {
Logger.getLogger(MessageSenderAsync.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void onException(Message msg, Exception e) {
try {
System.out.println(msg.getBody(String.class));
} catch (JMSException ex) {
Logger.getLogger(MessageSenderAsync.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
} catch (JMSRuntimeException ex) {
System.out.println("Caught RuntimeException trying to invoke setAsync - not permitted in Java EE. Resorting to synchronous sending...");
}
producer.send(asyncQueue, message);
}
Aggregations