Java Code Examples for org.apache.activemq.broker.BrokerService#addNetworkConnector()

The following examples show how to use org.apache.activemq.broker.BrokerService#addNetworkConnector() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreeBrokerStompTemporaryQueueTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
protected NetworkConnector bridgeBrokers(BrokerService localBroker,
                                         BrokerService remoteBroker,
                                         boolean dynamicOnly,
                                         int networkTTL,
                                         boolean conduit,
                                         boolean failover) throws Exception {
   List<TransportConnector> transportConnectors = remoteBroker.getTransportConnectors();
   URI remoteURI;
   if (!transportConnectors.isEmpty()) {
      remoteURI = transportConnectors.get(0).getConnectUri();
      NetworkConnector connector = new DiscoveryNetworkConnector(new URI("static:" + remoteURI));
      connector.setName(localBroker.getBrokerName() + remoteBroker.getBrokerName());
      localBroker.addNetworkConnector(connector);
      maxSetupTime = 2000;
      return connector;
   } else {
      throw new Exception("Remote broker has no registered connectors.");
   }
}
 
Example 2
Source File: TestBroker.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public TestBroker build() throws Exception {
    if (this.connectors.isEmpty()) {
        this.connectors.add(DEFAULT_BROKER_URL);
    }
    BrokerService brokerService = new BrokerService();
    brokerService.setBrokerName(this.brokerName);
    brokerService.setPersistent(false);
    brokerService.setUseJmx(false);
    Map<String, ActiveMQConnectionFactory> connectionFactories = new HashMap<String, ActiveMQConnectionFactory>();
    for (String bindAddress : this.connectors) {
        TransportConnector connector = brokerService.addConnector(bindAddress);
        connectionFactories.put(bindAddress, new ActiveMQConnectionFactory(connector.getConnectUri()));
    }
    for (String discoveryAddress : this.networkConnectors) {
        brokerService.addNetworkConnector(discoveryAddress);
    }
    return new TestBroker(this.brokerName, brokerService, connectionFactories);
}
 
Example 3
Source File: JmsMultipleBrokersTestSupport.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
protected void bridgeAllBrokers(String groupName,
                                int ttl,
                                boolean suppressduplicateQueueSubs,
                                boolean decreasePriority) throws Exception {
   Collection<BrokerItem> brokerList = brokers.values();
   for (Iterator<BrokerItem> i = brokerList.iterator(); i.hasNext(); ) {
      BrokerService broker = i.next().broker;
      List<TransportConnector> transportConnectors = broker.getTransportConnectors();

      if (transportConnectors.isEmpty()) {
         broker.addConnector(new URI(AUTO_ASSIGN_TRANSPORT));
         transportConnectors = broker.getTransportConnectors();
      }

      TransportConnector transport = transportConnectors.get(0);
      transport.setDiscoveryUri(new URI("multicast://default?group=" + groupName));
      NetworkConnector nc = broker.addNetworkConnector("multicast://default?group=" + groupName);
      nc.setNetworkTTL(ttl);
      nc.setSuppressDuplicateQueueSubscriptions(suppressduplicateQueueSubs);
      nc.setDecreaseNetworkConsumerPriority(decreasePriority);
   }

   // Multicasting may take longer to setup
   maxSetupTime = 8000;
}
 
Example 4
Source File: TwoBrokerTopicSendReceiveUsingJavaConfigurationTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
protected ActiveMQConnectionFactory createSenderConnectionFactory() throws JMSException {
   try {
      sendBroker = new BrokerService();
      sendBroker.setBrokerName("sendBroker");
      sendBroker.setUseJmx(false);
      sendBroker.setPersistent(false);
      sendBroker.addConnector("tcp://localhost:62001");
      sendBroker.addNetworkConnector("static:failover:tcp://localhost:62002");
      sendBroker.start();

      ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:62001");
      return factory;
   } catch (Exception e) {
      e.printStackTrace();
      return null;
   }
}
 
Example 5
Source File: TwoBrokerTopicSendReceiveUsingJavaConfigurationTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
protected ActiveMQConnectionFactory createReceiverConnectionFactory() throws JMSException {
   try {
      receiveBroker = new BrokerService();
      receiveBroker.setBrokerName("receiveBroker");
      receiveBroker.setUseJmx(false);
      receiveBroker.setPersistent(false);
      receiveBroker.addConnector("tcp://localhost:62002");
      receiveBroker.addNetworkConnector("static:failover:tcp://localhost:62001");
      receiveBroker.start();

      ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:62002");
      return factory;
   } catch (Exception e) {
      e.printStackTrace();
      return null;
   }
}
 
Example 6
Source File: JmsMultipleBrokersTestSupport.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected NetworkConnector bridgeBrokers(BrokerService localBroker,
                                         BrokerService remoteBroker,
                                         boolean dynamicOnly,
                                         int networkTTL,
                                         boolean conduit,
                                         boolean failover) throws Exception {
   List<TransportConnector> transportConnectors = remoteBroker.getTransportConnectors();
   URI remoteURI;
   if (!transportConnectors.isEmpty()) {
      remoteURI = transportConnectors.get(0).getConnectUri();
      String uri = "static:(" + remoteURI + ")";
      if (failover) {
         uri = "static:(failover:(" + remoteURI + "))";
      }
      NetworkConnector connector = new DiscoveryNetworkConnector(new URI(uri));
      connector.setName("to-" + remoteBroker.getBrokerName());
      connector.setDynamicOnly(dynamicOnly);
      connector.setNetworkTTL(networkTTL);
      connector.setConduitSubscriptions(conduit);
      localBroker.addNetworkConnector(connector);
      maxSetupTime = 2000;
      return connector;
   } else {
      throw new Exception("Remote broker has no registered connectors.");
   }

}
 
Example 7
Source File: NetworkBrokerDetachTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected BrokerService createBroker() throws Exception {
   BrokerService broker = new BrokerService();
   broker.setBrokerName(BROKER_NAME);
   configureBroker(broker);
   broker.addConnector("tcp://localhost:61617");
   NetworkConnector networkConnector = broker.addNetworkConnector("static:(tcp://localhost:62617?wireFormat.maxInactivityDuration=500)?useExponentialBackOff=false");
   configureNetworkConnector(networkConnector);
   return broker;
}
 
Example 8
Source File: ActiveMQ5Factory.java    From tomee with Apache License 2.0 5 votes vote down vote up
private BrokerService newDefaultBroker(final URI uri) throws Exception {
    final URISupport.CompositeData compositeData = URISupport.parseComposite(uri);
    final Map<String, String> params = new HashMap<>(compositeData.getParameters());

    final BrokerService brokerService = newPatchedBrokerService();

    IntrospectionSupport.setProperties(brokerService, params);
    if (!params.isEmpty()) {
        String msg = "There are " + params.size()
                + " Broker options that couldn't be set on the BrokerService."
                + " Check the options are spelled correctly."
                + " Unknown parameters=[" + params + "]."
                + " This BrokerService cannot be started.";
        throw new IllegalArgumentException(msg);
    }

    if (compositeData.getPath() != null) {
        brokerService.setBrokerName(compositeData.getPath());
    }

    for (final URI component : compositeData.getComponents()) {
        if ("network".equals(component.getScheme())) {
            brokerService.addNetworkConnector(component.getSchemeSpecificPart());
        } else if ("proxy".equals(component.getScheme())) {
            brokerService.addProxyConnector(component.getSchemeSpecificPart());
        } else {
            brokerService.addConnector(component);
        }
    }
    return brokerService;
}
 
Example 9
Source File: AMQStackOverFlowTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private BrokerService createBrokerService(final String brokerName,
                                          final String uri1,
                                          final String uri2) throws Exception {
   final BrokerService brokerService = new BrokerService();

   brokerService.setBrokerName(brokerName);
   brokerService.setPersistent(false);
   brokerService.setUseJmx(true);

   final SystemUsage memoryManager = new SystemUsage();
   //memoryManager.getMemoryUsage().setLimit(10);
   brokerService.setSystemUsage(memoryManager);

   final List<PolicyEntry> policyEntries = new ArrayList<>();

   final PolicyEntry entry = new PolicyEntry();
   entry.setQueue(">");
   //entry.setMemoryLimit(1);
   policyEntries.add(entry);

   final PolicyMap policyMap = new PolicyMap();
   policyMap.setPolicyEntries(policyEntries);
   brokerService.setDestinationPolicy(policyMap);

   final TransportConnector tConnector = new TransportConnector();
   tConnector.setUri(new URI(uri1));
   tConnector.setName(brokerName + ".transportConnector");
   brokerService.addConnector(tConnector);

   if (uri2 != null) {
      final NetworkConnector nc = new DiscoveryNetworkConnector(new URI("static:" + uri2));
      nc.setBridgeTempDestinations(true);
      nc.setBrokerName(brokerName);
      //nc.setPrefetchSize(1);
      brokerService.addNetworkConnector(nc);
   }

   return brokerService;
}
 
Example 10
Source File: NoDuplicateOnTopicNetworkTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private BrokerService createAndStartBroker(String name, String addr) throws Exception {
   BrokerService broker = new BrokerService();
   broker.setDeleteAllMessagesOnStartup(true);
   broker.setBrokerName(name);
   broker.addConnector(addr).setDiscoveryUri(new URI(MULTICAST_DEFAULT));
   broker.setUseJmx(false);

   NetworkConnector networkConnector = broker.addNetworkConnector(MULTICAST_DEFAULT);
   networkConnector.setDecreaseNetworkConsumerPriority(true);
   networkConnector.setDynamicOnly(dynamicOnly);
   networkConnector.setNetworkTTL(ttl);
   networkConnector.setSuppressDuplicateTopicSubscriptions(suppressDuplicateTopicSubs);
   networkConnector.setConsumerPriorityBase(BASE_PRIORITY);
   networkConnector.addStaticallyIncludedDestination(new ActiveMQTopic("BeStaticallyIncluded"));

   PolicyMap policyMap = new PolicyMap();
   PolicyEntry policy = new PolicyEntry();
   policy.setDispatchPolicy(dispatchPolicy);
   // the audit will suppress the duplicates as it defaults to true so this test
   // checking for dups will fail. it is good to have it on in practice.
   policy.setEnableAudit(false);
   policyMap.put(new ActiveMQTopic(TOPIC_NAME), policy);
   broker.setDestinationPolicy(policyMap);
   broker.start();

   return broker;
}
 
Example 11
Source File: NetworkOfTwentyBrokersTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected void bridgeAllBrokers(String groupName,
                                int ttl,
                                boolean suppressduplicateQueueSubs,
                                boolean decreasePriority) throws Exception {
   Collection<BrokerItem> brokerList = brokers.values();
   for (Iterator<BrokerItem> i = brokerList.iterator(); i.hasNext(); ) {
      BrokerService broker = i.next().broker;
      List<TransportConnector> transportConnectors = broker.getTransportConnectors();

      if (transportConnectors.isEmpty()) {
         broker.addConnector(new URI(AUTO_ASSIGN_TRANSPORT));
         transportConnectors = broker.getTransportConnectors();
      }

      TransportConnector transport = transportConnectors.get(0);
      if (transport.getDiscoveryUri() == null) {
         transport.setDiscoveryUri(new URI("multicast://default?group=" + groupName));
      }

      List<NetworkConnector> networkConnectors = broker.getNetworkConnectors();
      if (networkConnectors.isEmpty()) {
         broker.addNetworkConnector("multicast://default?group=" + groupName);
         networkConnectors = broker.getNetworkConnectors();
      }

      NetworkConnector nc = networkConnectors.get(0);
      nc.setNetworkTTL(ttl);
      nc.setSuppressDuplicateQueueSubscriptions(suppressduplicateQueueSubs);
      nc.setDecreaseNetworkConsumerPriority(decreasePriority);
   }

   // Multicasting may take longer to setup
   maxSetupTime = 8000;
}
 
Example 12
Source File: FailoverClusterTestSupport.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected void addNetworkBridge(BrokerService answer,
                                String bridgeName,
                                String uri,
                                boolean duplex,
                                String destinationFilter) throws Exception {
   NetworkConnector network = answer.addNetworkConnector(uri);
   network.setName(bridgeName);
   network.setDuplex(duplex);
   if (destinationFilter != null && !destinationFilter.equals("")) {
      network.setDestinationFilter(bridgeName);
   }
}
 
Example 13
Source File: NetworkLoopBackTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoopbackOnDifferentUrlScheme() throws Exception {
   final BrokerService brokerServce = new BrokerService();
   brokerServce.setPersistent(false);

   TransportConnector transportConnector = brokerServce.addConnector("nio://0.0.0.0:0");
   // connection filter is bypassed when scheme is different
   final NetworkConnector networkConnector = brokerServce.addNetworkConnector("static:(tcp://" + transportConnector.getConnectUri().getHost() + ":" + transportConnector.getConnectUri().getPort() + ")");

   brokerServce.start();
   brokerServce.waitUntilStarted();

   try {
      Wait.waitFor(new Wait.Condition() {
         @Override
         public boolean isSatisified() throws Exception {
            return 1 == networkConnector.bridges.size();
         }
      });

      final DemandForwardingBridgeSupport loopbackBridge = (DemandForwardingBridgeSupport) networkConnector.bridges.values().iterator().next();
      assertTrue("nc started", networkConnector.isStarted());

      assertTrue("It should get disposed", Wait.waitFor(new Wait.Condition() {
         @Override
         public boolean isSatisified() throws Exception {
            return loopbackBridge.getRemoteBroker().isDisposed();
         }
      }));

      assertEquals("No peer brokers", 0, brokerServce.getBroker().getPeerBrokerInfos().length);

   } finally {
      brokerServce.stop();
   }
}
 
Example 14
Source File: FailoverStaticNetworkTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected BrokerService createBroker(String scheme,
                                     String listenPort,
                                     String[] networkToPorts,
                                     HashMap<String, String> networkProps) throws Exception {
   BrokerService broker = new BrokerService();
   broker.getManagementContext().setCreateConnector(false);
   broker.setSslContext(sslContext);
   broker.setDeleteAllMessagesOnStartup(true);
   broker.setBrokerName("Broker_" + listenPort);
   // lazy init listener on broker start
   TransportConnector transportConnector = new TransportConnector();
   transportConnector.setUri(new URI(scheme + "://localhost:" + listenPort));
   List<TransportConnector> transportConnectors = new ArrayList<>();
   transportConnectors.add(transportConnector);
   broker.setTransportConnectors(transportConnectors);
   if (networkToPorts != null && networkToPorts.length > 0) {
      StringBuilder builder = new StringBuilder("static:(failover:(" + scheme + "://localhost:");
      builder.append(networkToPorts[0]);
      for (int i = 1; i < networkToPorts.length; i++) {
         builder.append("," + scheme + "://localhost:" + networkToPorts[i]);
      }
      // limit the reconnects in case of initial random connection to slave
      // leaving randomize on verifies that this config is picked up
      builder.append(")?maxReconnectAttempts=0)?useExponentialBackOff=false");
      NetworkConnector nc = broker.addNetworkConnector(builder.toString());
      if (networkProps != null) {
         IntrospectionSupport.setProperties(nc, networkProps);
      }
   }
   return broker;
}
 
Example 15
Source File: DuplexNetworkMBeanTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected BrokerService createNetworkedBroker() throws Exception {
   BrokerService broker = new BrokerService();
   broker.setBrokerName("networkedBroker");
   broker.addConnector("tcp://localhost:62617?transport.reuseAddress=true");
   NetworkConnector networkConnector = broker.addNetworkConnector("static:(tcp://localhost:61617?wireFormat.maxInactivityDuration=500)?useExponentialBackOff=false");
   networkConnector.setDuplex(true);
   return broker;
}
 
Example 16
Source File: NetworkBrokerDetachTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected BrokerService createNetworkedBroker() throws Exception {
   BrokerService broker = new BrokerService();
   broker.setBrokerName(REM_BROKER_NAME);
   configureBroker(broker);
   broker.getManagementContext().setCreateConnector(false);
   broker.addConnector("tcp://localhost:62617");
   NetworkConnector networkConnector = broker.addNetworkConnector("static:(tcp://localhost:61617?wireFormat.maxInactivityDuration=500)?useExponentialBackOff=false");
   configureNetworkConnector(networkConnector);
   return broker;
}
 
Example 17
Source File: DurableSubscriberWithNetworkDisconnectTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
protected NetworkConnector bridgeBrokers(BrokerService localBroker,
                                         BrokerService remoteBroker,
                                         boolean l_dynamicOnly,
                                         int networkTTL,
                                         boolean l_conduit,
                                         boolean l_failover) throws Exception {
   List<TransportConnector> transportConnectors = remoteBroker.getTransportConnectors();
   URI remoteURI;
   if (!transportConnectors.isEmpty()) {
      remoteURI = transportConnectors.get(0).getConnectUri();
      if (useSocketProxy) {
         socketProxy = new SocketProxy(remoteURI);
         remoteURI = socketProxy.getUrl();
      }
      String options = "";
      if (failover) {
         options = "static:(failover:(" + remoteURI;
      } else {
         options = "static:(" + remoteURI;
      }
      if (inactivity) {
         options += "?wireFormat.maxInactivityDuration=" + inactiveDuration + "&wireFormat.maxInactivityDurationInitalDelay=" + inactiveDuration + ")";
      } else {
         options += ")";
      }

      if (failover) {
         options += "?maxReconnectAttempts=0)";
      }

      options += "?useExponentialBackOff=" + exponentialBackOff;
      DiscoveryNetworkConnector connector = new DiscoveryNetworkConnector(new URI(options));
      connector.setDynamicOnly(dynamicOnly);
      connector.setNetworkTTL(networkTTL);
      localBroker.addNetworkConnector(connector);
      maxSetupTime = 2000;
      if (useDuplexNetworkBridge) {
         connector.setDuplex(true);
      }
      return connector;
   } else {
      throw new Exception("Remote broker has no registered connectors.");
   }
}
 
Example 18
Source File: TraceBrokerPathPluginTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
   TraceBrokerPathPlugin tbppA = new TraceBrokerPathPlugin();
   tbppA.setStampProperty(traceProperty);

   TraceBrokerPathPlugin tbppB = new TraceBrokerPathPlugin();
   tbppB.setStampProperty(traceProperty);

   brokerA = new BrokerService();
   brokerA.setBrokerName("brokerA");
   brokerA.setPersistent(false);
   brokerA.setUseJmx(true);
   brokerA.setPlugins(new BrokerPlugin[]{tbppA});
   tcpConnectorA = brokerA.addConnector("tcp://localhost:0");

   brokerB = new BrokerService();
   brokerB.setBrokerName("brokerB");
   brokerB.setPersistent(false);
   brokerB.setUseJmx(true);
   brokerB.setPlugins(new BrokerPlugin[]{tbppB});
   tcpConnectorB = brokerB.addConnector("tcp://localhost:0");

   brokerA.addNetworkConnector("static:(" + tcpConnectorB.getConnectUri().toString() + ")");

   brokerB.start();
   brokerB.waitUntilStarted();
   brokerA.start();
   brokerA.waitUntilStarted();

   // Initialise connection to A and MessageProducer
   connectionA = new ActiveMQConnectionFactory(tcpConnectorA.getConnectUri()).createConnection();
   connectionA.start();
   sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
   producer = sessionA.createProducer(sessionA.createQueue(queue));
   producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

   // Initialise connection to B and MessageConsumer
   connectionB = new ActiveMQConnectionFactory(tcpConnectorB.getConnectUri()).createConnection();
   connectionB.start();
   sessionB = connectionB.createSession(false, Session.AUTO_ACKNOWLEDGE);
   consumer = sessionB.createConsumer(sessionB.createQueue(queue));

}
 
Example 19
Source File: NetworkAsyncStartTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private void bridgeBroker(BrokerService localBroker, String remoteURI) throws Exception {
   String uri = "static:(" + remoteURI + ")";
   NetworkConnector connector = new DiscoveryNetworkConnector(new URI(uri));
   connector.setName("bridge-" + bridgeCount++);
   localBroker.addNetworkConnector(connector);
}
 
Example 20
Source File: NetworkLoadTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
protected BrokerService createBroker(int brokerId) throws Exception {
   BrokerService broker = new BrokerService();
   broker.setBrokerName("broker-" + brokerId);
   broker.setPersistent(false);
   broker.setUseJmx(true);
   broker.getManagementContext().setCreateConnector(false);

   final SystemUsage memoryManager = new SystemUsage();
   memoryManager.getMemoryUsage().setLimit(1024 * 1024 * 50); // 50 MB
   broker.setSystemUsage(memoryManager);

   final List<PolicyEntry> policyEntries = new ArrayList<>();
   final PolicyEntry entry = new PolicyEntry();
   entry.setQueue(">");
   entry.setMemoryLimit(1024 * 1024 * 1); // Set to 1 MB
   entry.setPendingSubscriberPolicy(new VMPendingSubscriberMessageStoragePolicy());
   entry.setPendingQueuePolicy(new VMPendingQueueMessageStoragePolicy());
   policyEntries.add(entry);

   // This is to turn of the default behavior of storing topic messages for retroactive consumption
   final PolicyEntry topicPolicyEntry = new PolicyEntry();
   topicPolicyEntry.setTopic(">");
   final NoSubscriptionRecoveryPolicy noSubscriptionRecoveryPolicy = new NoSubscriptionRecoveryPolicy();
   topicPolicyEntry.setSubscriptionRecoveryPolicy(noSubscriptionRecoveryPolicy);

   final PolicyMap policyMap = new PolicyMap();
   policyMap.setPolicyEntries(policyEntries);
   broker.setDestinationPolicy(policyMap);

   TransportConnector transportConnector = new TransportConnector();
   transportConnector.setUri(new URI("tcp://localhost:" + (60000 + brokerId)));

   transportConnector.setDiscoveryUri(new URI("multicast://default?group=" + groupId));
   broker.addConnector(transportConnector);

   DiscoveryNetworkConnector networkConnector = new DiscoveryNetworkConnector();
   networkConnector.setUri(new URI("multicast://default?group=" + groupId));
   networkConnector.setBridgeTempDestinations(true);
   networkConnector.setPrefetchSize(1);
   broker.addNetworkConnector(networkConnector);

   return broker;
}