Search in sources :

Example 11 with ActiveMQJMSConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory in project activemq-artemis by apache.

the class JmsSend method main.

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ActiveMQJMSConnectionFactory("tcp://localhost:61616");
    Destination destination = ActiveMQDestination.fromPrefixedName("queue://orders");
    try (Connection conn = factory.createConnection()) {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(destination);
        ObjectMessage message = session.createObjectMessage();
        Order order = new Order("Bill", "$199.99", "iPhone4");
        message.setObject(order);
        producer.send(message);
    }
}
Also used : Destination(javax.jms.Destination) ActiveMQDestination(org.apache.activemq.artemis.jms.client.ActiveMQDestination) ActiveMQJMSConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQJMSConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory) ObjectMessage(javax.jms.ObjectMessage) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 12 with ActiveMQJMSConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory in project activemq-artemis by apache.

the class JMSBridgeImplTest method testStartWithRepeatedFailure.

// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testStartWithRepeatedFailure() throws Exception {
    ActiveMQJMSConnectionFactory failingSourceCF = new ActiveMQJMSConnectionFactory(false, new TransportConfiguration(InVMConnectorFactory.class.getName())) {

        private static final long serialVersionUID = 2834937512213001068L;

        @Override
        public Connection createConnection() throws JMSException {
            throw new JMSException("unable to create a conn");
        }
    };
    ConnectionFactoryFactory sourceCFF = JMSBridgeImplTest.newConnectionFactoryFactory(failingSourceCF);
    ConnectionFactoryFactory targetCFF = JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    DestinationFactory sourceDF = JMSBridgeImplTest.newDestinationFactory(ActiveMQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
    DestinationFactory targetDF = JMSBridgeImplTest.newDestinationFactory(ActiveMQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
    TransactionManager tm = JMSBridgeImplTest.newTransactionManager();
    JMSBridgeImpl bridge = new JMSBridgeImpl();
    bridge.setSourceConnectionFactoryFactory(sourceCFF);
    bridge.setSourceDestinationFactory(sourceDF);
    bridge.setTargetConnectionFactoryFactory(targetCFF);
    bridge.setTargetDestinationFactory(targetDF);
    // retry after 10 ms
    bridge.setFailureRetryInterval(10);
    // retry only once
    bridge.setMaxRetries(1);
    bridge.setMaxBatchSize(1);
    bridge.setMaxBatchTime(-1);
    bridge.setTransactionManager(tm);
    bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);
    Assert.assertFalse(bridge.isStarted());
    bridge.start();
    Thread.sleep(50);
    Assert.assertFalse(bridge.isStarted());
    Assert.assertTrue(bridge.isFailed());
    bridge.stop();
}
Also used : DestinationFactory(org.apache.activemq.artemis.jms.bridge.DestinationFactory) JMSBridgeImpl(org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl) ActiveMQJMSConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory) TransactionManager(javax.transaction.TransactionManager) ConnectionFactoryFactory(org.apache.activemq.artemis.jms.bridge.ConnectionFactoryFactory) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 13 with ActiveMQJMSConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory in project activemq-artemis by apache.

the class JMSBridgeImplTest method createConnectionFactory.

private static ConnectionFactory createConnectionFactory() {
    ActiveMQJMSConnectionFactory cf = (ActiveMQJMSConnectionFactory) ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(InVMConnectorFactory.class.getName()));
    // Note! We disable automatic reconnection on the session factory. The bridge needs to do the reconnection
    cf.setReconnectAttempts(0);
    cf.setBlockOnNonDurableSend(true);
    cf.setBlockOnDurableSend(true);
    return cf;
}
Also used : ActiveMQJMSConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) InVMConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory)

Example 14 with ActiveMQJMSConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory in project activemq-artemis by apache.

the class JMSBridgeImplTest method testExceptionOnSourceAndRetryFails.

@Test
public void testExceptionOnSourceAndRetryFails() throws Exception {
    final AtomicReference<Connection> sourceConn = new AtomicReference<>();
    ActiveMQJMSConnectionFactory failingSourceCF = new ActiveMQJMSConnectionFactory(false, new TransportConfiguration(INVM_CONNECTOR_FACTORY)) {

        private static final long serialVersionUID = 8216804886099984645L;

        boolean firstTime = true;

        @Override
        public Connection createConnection() throws JMSException {
            if (firstTime) {
                firstTime = false;
                sourceConn.set(super.createConnection());
                return sourceConn.get();
            } else {
                throw new JMSException("exception while retrying to connect");
            }
        }
    };
    // Note! We disable automatic reconnection on the session factory. The bridge needs to do the reconnection
    failingSourceCF.setReconnectAttempts(0);
    failingSourceCF.setBlockOnNonDurableSend(true);
    failingSourceCF.setBlockOnDurableSend(true);
    ConnectionFactoryFactory sourceCFF = JMSBridgeImplTest.newConnectionFactoryFactory(failingSourceCF);
    ConnectionFactoryFactory targetCFF = JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    DestinationFactory sourceDF = JMSBridgeImplTest.newDestinationFactory(ActiveMQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
    DestinationFactory targetDF = JMSBridgeImplTest.newDestinationFactory(ActiveMQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
    TransactionManager tm = JMSBridgeImplTest.newTransactionManager();
    JMSBridgeImpl bridge = new JMSBridgeImpl();
    Assert.assertNotNull(bridge);
    bridge.setSourceConnectionFactoryFactory(sourceCFF);
    bridge.setSourceDestinationFactory(sourceDF);
    bridge.setTargetConnectionFactoryFactory(targetCFF);
    bridge.setTargetDestinationFactory(targetDF);
    bridge.setFailureRetryInterval(100);
    bridge.setMaxRetries(1);
    bridge.setMaxBatchSize(1);
    bridge.setMaxBatchTime(-1);
    bridge.setTransactionManager(tm);
    bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);
    Assert.assertFalse(bridge.isStarted());
    bridge.start();
    Assert.assertTrue(bridge.isStarted());
    sourceConn.get().getExceptionListener().onException(new JMSException("exception on the source"));
    Thread.sleep(4 * bridge.getFailureRetryInterval());
    // reconnection must have failed
    Assert.assertFalse(bridge.isStarted());
}
Also used : DestinationFactory(org.apache.activemq.artemis.jms.bridge.DestinationFactory) JMSBridgeImpl(org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl) ActiveMQJMSConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory) TransactionManager(javax.transaction.TransactionManager) Connection(javax.jms.Connection) ConnectionFactoryFactory(org.apache.activemq.artemis.jms.bridge.ConnectionFactoryFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 15 with ActiveMQJMSConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory in project activemq-artemis by apache.

the class JMSBridgeImplTest method testExceptionOnSourceAndRetrySucceeds.

@Test
public void testExceptionOnSourceAndRetrySucceeds() throws Exception {
    final AtomicReference<Connection> sourceConn = new AtomicReference<>();
    ActiveMQJMSConnectionFactory failingSourceCF = new ActiveMQJMSConnectionFactory(false, new TransportConfiguration(InVMConnectorFactory.class.getName())) {

        private static final long serialVersionUID = -8866390811966688830L;

        @Override
        public Connection createConnection() throws JMSException {
            sourceConn.set(super.createConnection());
            return sourceConn.get();
        }
    };
    // Note! We disable automatic reconnection on the session factory. The bridge needs to do the reconnection
    failingSourceCF.setReconnectAttempts(0);
    failingSourceCF.setBlockOnNonDurableSend(true);
    failingSourceCF.setBlockOnDurableSend(true);
    ConnectionFactoryFactory sourceCFF = JMSBridgeImplTest.newConnectionFactoryFactory(failingSourceCF);
    ConnectionFactoryFactory targetCFF = JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
    DestinationFactory sourceDF = JMSBridgeImplTest.newDestinationFactory(ActiveMQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
    DestinationFactory targetDF = JMSBridgeImplTest.newDestinationFactory(ActiveMQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
    TransactionManager tm = JMSBridgeImplTest.newTransactionManager();
    JMSBridgeImpl bridge = new JMSBridgeImpl();
    Assert.assertNotNull(bridge);
    bridge.setSourceConnectionFactoryFactory(sourceCFF);
    bridge.setSourceDestinationFactory(sourceDF);
    bridge.setTargetConnectionFactoryFactory(targetCFF);
    bridge.setTargetDestinationFactory(targetDF);
    bridge.setFailureRetryInterval(10);
    bridge.setMaxRetries(2);
    bridge.setMaxBatchSize(1);
    bridge.setMaxBatchTime(-1);
    bridge.setTransactionManager(tm);
    bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);
    Assert.assertFalse(bridge.isStarted());
    bridge.start();
    Assert.assertTrue(bridge.isStarted());
    sourceConn.get().getExceptionListener().onException(new JMSException("exception on the source"));
    Thread.sleep(4 * bridge.getFailureRetryInterval());
    // reconnection must have succeeded
    Assert.assertTrue(bridge.isStarted());
    bridge.stop();
    Assert.assertFalse(bridge.isStarted());
}
Also used : DestinationFactory(org.apache.activemq.artemis.jms.bridge.DestinationFactory) JMSBridgeImpl(org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl) ActiveMQJMSConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory) TransactionManager(javax.transaction.TransactionManager) Connection(javax.jms.Connection) ConnectionFactoryFactory(org.apache.activemq.artemis.jms.bridge.ConnectionFactoryFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Aggregations

ActiveMQJMSConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory)24 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)13 Connection (javax.jms.Connection)12 Test (org.junit.Test)11 Session (javax.jms.Session)9 ConnectionFactory (javax.jms.ConnectionFactory)8 MessageProducer (javax.jms.MessageProducer)7 Destination (javax.jms.Destination)6 JMSException (javax.jms.JMSException)6 ConnectionFactoryFactory (org.apache.activemq.artemis.jms.bridge.ConnectionFactoryFactory)6 DestinationFactory (org.apache.activemq.artemis.jms.bridge.DestinationFactory)6 TransactionManager (javax.transaction.TransactionManager)5 JMSBridgeImpl (org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl)5 ActiveMQDestination (org.apache.activemq.artemis.jms.client.ActiveMQDestination)5 MessageConsumer (javax.jms.MessageConsumer)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ObjectMessage (javax.jms.ObjectMessage)3 TextMessage (javax.jms.TextMessage)3 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)3 HashMap (java.util.HashMap)2