Search in sources :

Example 1 with JMSBridge

use of org.apache.activemq.artemis.jms.bridge.JMSBridge 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 JMSBridge

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

the class JMSBridgeHandler method executeRuntimeStep.

@Override
protected void executeRuntimeStep(OperationContext context, ModelNode operation) throws OperationFailedException {
    final String bridgeName = context.getCurrentAddressValue();
    final String operationName = operation.require(OP).asString();
    if (null == operationName) {
        throw MessagingLogger.ROOT_LOGGER.unsupportedOperation(operationName);
    }
    final boolean modify = !READ_ATTRIBUTE_OPERATION.equals(operationName);
    final ServiceName bridgeServiceName = MessagingServices.getJMSBridgeServiceName(bridgeName);
    final ServiceController<?> bridgeService = context.getServiceRegistry(modify).getService(bridgeServiceName);
    if (bridgeService == null) {
        if (!readOnly) {
            throw ControllerLogger.ROOT_LOGGER.managementResourceNotFound(context.getCurrentAddress());
        }
        return;
    }
    final JMSBridge bridge = JMSBridge.class.cast(bridgeService.getValue());
    switch(operationName) {
        case READ_ATTRIBUTE_OPERATION:
            readAttributeValidator.validate(operation);
            final String name = operation.require(NAME).asString();
            if (STARTED.equals(name)) {
                context.getResult().set(bridge.isStarted());
            } else if (PAUSED.getName().equals(name)) {
                context.getResult().set(bridge.isPaused());
            } else if (CommonAttributes.MESSAGE_COUNT.getName().equals(name)) {
                context.getResult().set(bridge.getMessageCount());
            } else if (ABORTED_MESSAGE_COUNT.getName().equals(name)) {
                context.getResult().set(bridge.getAbortedMessageCount());
            } else {
                throw MessagingLogger.ROOT_LOGGER.unsupportedAttribute(name);
            }
            break;
        case START:
            try {
                // we do not start the bridge directly but call startBridge() instead
                // to ensure the class loader will be able to load any external resources
                JMSBridgeService service = (JMSBridgeService) bridgeService.getService();
                service.startBridge();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            break;
        case STOP:
            try {
                bridge.stop();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            break;
        case PAUSE:
            try {
                bridge.pause();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            break;
        case RESUME:
            try {
                bridge.resume();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            break;
        default:
            throw MessagingLogger.ROOT_LOGGER.unsupportedOperation(operationName);
    }
    context.completeStep(new OperationContext.RollbackHandler() {

        @Override
        public void handleRollback(OperationContext context, ModelNode operation) {
            try {
                switch(operationName) {
                    case START:
                        bridge.stop();
                        break;
                    case STOP:
                        JMSBridgeService service = (JMSBridgeService) bridgeService.getService();
                        service.startBridge();
                        break;
                    case PAUSE:
                        bridge.resume();
                        break;
                    case RESUME:
                        bridge.pause();
                        break;
                }
            } catch (Exception e) {
                MessagingLogger.ROOT_LOGGER.revertOperationFailed(e, getClass().getSimpleName(), operationName, context.getCurrentAddress());
            }
        }
    });
}
Also used : OperationContext(org.jboss.as.controller.OperationContext) ServiceName(org.jboss.msc.service.ServiceName) JMSBridge(org.apache.activemq.artemis.jms.bridge.JMSBridge) ModelNode(org.jboss.dmr.ModelNode) OperationFailedException(org.jboss.as.controller.OperationFailedException)

Aggregations

JMSBridge (org.apache.activemq.artemis.jms.bridge.JMSBridge)2 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 JMSBridgeImpl (org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl)1 JNDIConnectionFactoryFactory (org.apache.activemq.artemis.jms.bridge.impl.JNDIConnectionFactoryFactory)1 JNDIDestinationFactory (org.apache.activemq.artemis.jms.bridge.impl.JNDIDestinationFactory)1 OperationContext (org.jboss.as.controller.OperationContext)1 OperationFailedException (org.jboss.as.controller.OperationFailedException)1 ModelNode (org.jboss.dmr.ModelNode)1 ServiceName (org.jboss.msc.service.ServiceName)1