Search in sources :

Example 1 with JNDIConnectionFactoryFactory

use of org.apache.activemq.artemis.jms.bridge.impl.JNDIConnectionFactoryFactory in project activemq-artemis by apache.

the class JMSBridgeExample method main.

public static void main(final String[] args) throws Exception {
    String sourceServer = "tcp://localhost:61616";
    String targetServer = "tcp://localhost:61617";
    System.out.println("client will publish messages to " + sourceServer + " and receives message from " + targetServer);
    // Step 1. Create JNDI contexts for source and target servers
    InitialContext sourceContext = JMSBridgeExample.createContext(sourceServer);
    InitialContext targetContext = JMSBridgeExample.createContext(targetServer);
    Hashtable<String, String> sourceJndiParams = createJndiParams(sourceServer);
    Hashtable<String, String> targetJndiParams = createJndiParams(targetServer);
    // Step 2. Create and start a JMS Bridge
    // Note, the Bridge needs a transaction manager, in this instance we will use the JBoss TM
    JMSBridge jmsBridge = new JMSBridgeImpl(new JNDIConnectionFactoryFactory(sourceJndiParams, "ConnectionFactory"), new JNDIConnectionFactoryFactory(targetJndiParams, "ConnectionFactory"), new JNDIDestinationFactory(sourceJndiParams, "source/topic"), new JNDIDestinationFactory(targetJndiParams, "target/queue"), null, null, null, null, null, 5000, 10, QualityOfServiceMode.DUPLICATES_OK, 1, -1, null, null, true);
    Connection sourceConnection = null;
    Connection targetConnection = null;
    try {
        jmsBridge.start();
        // Step 3. Lookup the *source* JMS resources
        ConnectionFactory sourceConnectionFactory = (ConnectionFactory) sourceContext.lookup("ConnectionFactory");
        Topic sourceTopic = (Topic) sourceContext.lookup("source/topic");
        // Step 4. Create a connection, a session and a message producer for the *source* topic
        sourceConnection = sourceConnectionFactory.createConnection();
        Session sourceSession = sourceConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sourceProducer = sourceSession.createProducer(sourceTopic);
        // Step 5. Create and send a text message to the *source* queue
        TextMessage message = sourceSession.createTextMessage("this is a text message sent at " + System.currentTimeMillis());
        sourceProducer.send(message);
        System.out.format("Sent message to %s: %s%n", ((Topic) message.getJMSDestination()).getTopicName(), message.getText());
        System.out.format("Message ID : %s%n", message.getJMSMessageID());
        // Step 6. Close the *source* connection
        sourceConnection.close();
        // Step 7. Lookup the *target* JMS resources
        ConnectionFactory targetConnectionFactory = (ConnectionFactory) targetContext.lookup("ConnectionFactory");
        Queue targetQueue = (Queue) targetContext.lookup("target/queue");
        // Step 8. Create a connection, a session and a message consumer for the *target* queue
        targetConnection = targetConnectionFactory.createConnection();
        Session targetSession = targetConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer targetConsumer = targetSession.createConsumer(targetQueue);
        // Step 9. Start the connection to receive messages from the *target* queue
        targetConnection.start();
        // Step 10. Receive a message from the *target* queue
        TextMessage messageReceived = (TextMessage) targetConsumer.receive(500000);
        System.out.format("%nReceived from %s: %s%n", ((Queue) messageReceived.getJMSDestination()).getQueueName(), messageReceived.getText());
        // Step 11. Display the received message's ID and this "bridged" message ID
        System.out.format("Message ID         : %s%n", messageReceived.getJMSMessageID());
        System.out.format("Bridged Message ID : %s%n", messageReceived.getStringProperty("AMQ_BRIDGE_MSG_ID_LIST"));
    } finally {
        // Step 12. Be sure to close the resources!
        jmsBridge.stop();
        if (sourceContext != null) {
            sourceContext.close();
        }
        if (targetContext != null) {
            targetContext.close();
        }
        if (sourceConnection != null) {
            sourceConnection.close();
        }
        if (targetConnection != null) {
            targetConnection.close();
        }
    }
}
Also used : JNDIConnectionFactoryFactory(org.apache.activemq.artemis.jms.bridge.impl.JNDIConnectionFactoryFactory) MessageConsumer(javax.jms.MessageConsumer) JMSBridgeImpl(org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl) JMSBridge(org.apache.activemq.artemis.jms.bridge.JMSBridge) Connection(javax.jms.Connection) InitialContext(javax.naming.InitialContext) ConnectionFactory(javax.jms.ConnectionFactory) JNDIDestinationFactory(org.apache.activemq.artemis.jms.bridge.impl.JNDIDestinationFactory) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 2 with JNDIConnectionFactoryFactory

use of org.apache.activemq.artemis.jms.bridge.impl.JNDIConnectionFactoryFactory in project wildfly by wildfly.

the class JMSBridgeAdd method createJMSBridge.

private JMSBridge createJMSBridge(OperationContext context, ModelNode model) throws OperationFailedException {
    final Properties sourceContextProperties = resolveContextProperties(JMSBridgeDefinition.SOURCE_CONTEXT, context, model);
    final String sourceConnectionFactoryName = JMSBridgeDefinition.SOURCE_CONNECTION_FACTORY.resolveModelAttribute(context, model).asString();
    final ConnectionFactoryFactory sourceCff = new JNDIConnectionFactoryFactory(sourceContextProperties, sourceConnectionFactoryName);
    final String sourceDestinationName = JMSBridgeDefinition.SOURCE_DESTINATION.resolveModelAttribute(context, model).asString();
    final DestinationFactory sourceDestinationFactory = new JNDIDestinationFactory(sourceContextProperties, sourceDestinationName);
    final Properties targetContextProperties = resolveContextProperties(JMSBridgeDefinition.TARGET_CONTEXT, context, model);
    final String targetConnectionFactoryName = JMSBridgeDefinition.TARGET_CONNECTION_FACTORY.resolveModelAttribute(context, model).asString();
    final ConnectionFactoryFactory targetCff = new JNDIConnectionFactoryFactory(targetContextProperties, targetConnectionFactoryName);
    final String targetDestinationName = JMSBridgeDefinition.TARGET_DESTINATION.resolveModelAttribute(context, model).asString();
    final DestinationFactory targetDestinationFactory = new JNDIDestinationFactory(targetContextProperties, targetDestinationName);
    final String sourceUsername = resolveAttribute(JMSBridgeDefinition.SOURCE_USER, context, model);
    final String sourcePassword = resolveAttribute(JMSBridgeDefinition.SOURCE_PASSWORD, context, model);
    final String targetUsername = resolveAttribute(JMSBridgeDefinition.TARGET_USER, context, model);
    final String targetPassword = resolveAttribute(JMSBridgeDefinition.TARGET_PASSWORD, context, model);
    final String selector = resolveAttribute(CommonAttributes.SELECTOR, context, model);
    final long failureRetryInterval = JMSBridgeDefinition.FAILURE_RETRY_INTERVAL.resolveModelAttribute(context, model).asLong();
    final int maxRetries = JMSBridgeDefinition.MAX_RETRIES.resolveModelAttribute(context, model).asInt();
    final QualityOfServiceMode qosMode = QualityOfServiceMode.valueOf(JMSBridgeDefinition.QUALITY_OF_SERVICE.resolveModelAttribute(context, model).asString());
    final int maxBatchSize = JMSBridgeDefinition.MAX_BATCH_SIZE.resolveModelAttribute(context, model).asInt();
    final long maxBatchTime = JMSBridgeDefinition.MAX_BATCH_TIME.resolveModelAttribute(context, model).asLong();
    final String subName = resolveAttribute(JMSBridgeDefinition.SUBSCRIPTION_NAME, context, model);
    final String clientID = resolveAttribute(JMSBridgeDefinition.CLIENT_ID, context, model);
    final boolean addMessageIDInHeader = JMSBridgeDefinition.ADD_MESSAGE_ID_IN_HEADER.resolveModelAttribute(context, model).asBoolean();
    final String moduleName = resolveAttribute(JMSBridgeDefinition.MODULE, context, model);
    final ClassLoader oldTccl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
    try {
        // will use the correct class loader to execute its threads
        if (moduleName != null) {
            Module module = Module.getCallerModuleLoader().loadModule(ModuleIdentifier.fromString(moduleName));
            WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(module.getClassLoader());
        }
        return new JMSBridgeImpl(sourceCff, targetCff, sourceDestinationFactory, targetDestinationFactory, sourceUsername, sourcePassword, targetUsername, targetPassword, selector, failureRetryInterval, maxRetries, qosMode, maxBatchSize, maxBatchTime, subName, clientID, addMessageIDInHeader).setBridgeName(context.getCurrentAddressValue());
    } catch (ModuleNotFoundException e) {
        throw MessagingLogger.ROOT_LOGGER.moduleNotFound(moduleName, e.getMessage(), e);
    } catch (ModuleLoadException e) {
        throw MessagingLogger.ROOT_LOGGER.unableToLoadModule(moduleName, e);
    } finally {
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTccl);
    }
}
Also used : ModuleLoadException(org.jboss.modules.ModuleLoadException) JNDIDestinationFactory(org.apache.activemq.artemis.jms.bridge.impl.JNDIDestinationFactory) DestinationFactory(org.apache.activemq.artemis.jms.bridge.DestinationFactory) JNDIConnectionFactoryFactory(org.apache.activemq.artemis.jms.bridge.impl.JNDIConnectionFactoryFactory) ModuleNotFoundException(org.jboss.modules.ModuleNotFoundException) JMSBridgeImpl(org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl) QualityOfServiceMode(org.apache.activemq.artemis.jms.bridge.QualityOfServiceMode) Properties(java.util.Properties) JNDIDestinationFactory(org.apache.activemq.artemis.jms.bridge.impl.JNDIDestinationFactory) JNDIConnectionFactoryFactory(org.apache.activemq.artemis.jms.bridge.impl.JNDIConnectionFactoryFactory) ConnectionFactoryFactory(org.apache.activemq.artemis.jms.bridge.ConnectionFactoryFactory) Module(org.jboss.modules.Module)

Aggregations

JMSBridgeImpl (org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl)2 JNDIConnectionFactoryFactory (org.apache.activemq.artemis.jms.bridge.impl.JNDIConnectionFactoryFactory)2 JNDIDestinationFactory (org.apache.activemq.artemis.jms.bridge.impl.JNDIDestinationFactory)2 Properties (java.util.Properties)1 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 MessageConsumer (javax.jms.MessageConsumer)1 MessageProducer (javax.jms.MessageProducer)1 Queue (javax.jms.Queue)1 Session (javax.jms.Session)1 TextMessage (javax.jms.TextMessage)1 Topic (javax.jms.Topic)1 InitialContext (javax.naming.InitialContext)1 ConnectionFactoryFactory (org.apache.activemq.artemis.jms.bridge.ConnectionFactoryFactory)1 DestinationFactory (org.apache.activemq.artemis.jms.bridge.DestinationFactory)1 JMSBridge (org.apache.activemq.artemis.jms.bridge.JMSBridge)1 QualityOfServiceMode (org.apache.activemq.artemis.jms.bridge.QualityOfServiceMode)1 Module (org.jboss.modules.Module)1 ModuleLoadException (org.jboss.modules.ModuleLoadException)1 ModuleNotFoundException (org.jboss.modules.ModuleNotFoundException)1