public void startBridge() throws Exception {
   if (moduleName == null) {
     bridge.start();
   } else {
     ClassLoader oldTccl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
     try {
       ModuleIdentifier moduleID = ModuleIdentifier.create(moduleName);
       Module module = Module.getCallerModuleLoader().loadModule(moduleID);
       WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(module.getClassLoader());
       bridge.start();
     } finally {
       WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTccl);
     }
   }
   MessagingLogger.MESSAGING_LOGGER.startedService("JMS Bridge", bridgeName);
 }
Beispiel #2
0
  public static void main(final String[] args) throws Exception {
    if (args.length != 2) {
      throw new IllegalArgumentException(
          "JMSBridgeExample needs 2 arguments: <source server> <target server>");
    }
    String sourceServer = args[0];
    String targetServer = args[1];

    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, "/source/ConnectionFactory"),
            new JNDIConnectionFactoryFactory(targetJndiParams, "/target/ConnectionFactory"),
            new JNDIDestinationFactory(sourceJndiParams, "/source/topic"),
            new JNDIDestinationFactory(targetJndiParams, "/target/queue"),
            null,
            null,
            null,
            null,
            null,
            5000,
            10,
            QualityOfServiceMode.ONCE_AND_ONLY_ONCE,
            1,
            -1,
            null,
            null,
            true);
    jmsBridge.setTransactionManager(new TransactionManagerImple());

    Connection sourceConnection = null;
    Connection targetConnection = null;
    try {
      jmsBridge.start();
      // Step 3. Lookup the *source* JMS resources
      ConnectionFactory sourceConnectionFactory =
          (ConnectionFactory) sourceContext.lookup("/client/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("/client/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(5000);
      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("HQ_BRIDGE_MSG_ID_LIST"));
    } finally {
      // Step 12. Be sure to close the resources!
      if (jmsBridge != null) {
        jmsBridge.stop();
      }
      if (sourceContext != null) {
        sourceContext.close();
      }
      if (targetContext != null) {
        targetContext.close();
      }
      if (sourceConnection != null) {
        sourceConnection.close();
      }
      if (targetConnection != null) {
        targetConnection.close();
      }
    }
  }