Search in sources :

Example 36 with JMSProducer

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 }
    }
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) InVMConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory) JMSException(javax.jms.JMSException) JMSProducer(javax.jms.JMSProducer) MessageProducer(javax.jms.MessageProducer) JMSContext(javax.jms.JMSContext) Session(javax.jms.Session) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 37 with JMSProducer

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();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.artemis.jms.client.ActiveMQTextMessage) Test(org.junit.Test)

Example 38 with JMSProducer

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");
}
Also used : JMSProducer(javax.jms.JMSProducer)

Example 39 with JMSProducer

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);
}
Also used : Message(javax.jms.Message) CompletionListener(javax.jms.CompletionListener) JMSProducer(javax.jms.JMSProducer) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) JMSRuntimeException(javax.jms.JMSRuntimeException) JMSRuntimeException(javax.jms.JMSRuntimeException)

Aggregations

JMSProducer (javax.jms.JMSProducer)39 Test (org.junit.Test)32 JMSContext (javax.jms.JMSContext)22 JMSConsumer (javax.jms.JMSConsumer)21 TextMessage (javax.jms.TextMessage)20 Message (javax.jms.Message)11 BytesMessage (javax.jms.BytesMessage)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)8 ConnectionFactory (javax.jms.ConnectionFactory)7 Destination (javax.jms.Destination)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 StreamMessage (javax.jms.StreamMessage)6 JMSException (javax.jms.JMSException)5 JMSRuntimeException (javax.jms.JMSRuntimeException)4 Queue (javax.jms.Queue)4 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)4 Topic (javax.jms.Topic)3 CompletionListener (javax.jms.CompletionListener)2 IllegalStateRuntimeException (javax.jms.IllegalStateRuntimeException)2 InvalidDestinationRuntimeException (javax.jms.InvalidDestinationRuntimeException)2