org.apache.activemq.artemis.core.server.cluster.Bridge

Here are the examples of the java api class org.apache.activemq.artemis.core.server.cluster.Bridge taken from open source projects.

1. BridgeReconnectTest#testReconnectSameNode()

View license
// Fail bridge and reconnect same node, no backup specified
@Test
public void testReconnectSameNode() throws Exception {
    server0 = createActiveMQServer(0, isNetty(), server0Params);
    TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params, "server0tc");
    server0.getConfiguration().setConnectorConfigurations(connectors);
    server1.getConfiguration().setConnectorConfigurations(connectors);
    BridgeConfiguration bridgeConfiguration = createBridgeConfig();
    List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
    bridgeConfigs.add(bridgeConfiguration);
    server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
    CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName);
    List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
    queueConfigs0.add(queueConfig0);
    server0.getConfiguration().setQueueConfigurations(queueConfigs0);
    CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName);
    List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
    queueConfigs1.add(queueConfig1);
    server1.getConfiguration().setQueueConfigurations(queueConfigs1);
    startServers();
    locator = addServerLocator(ActiveMQClient.createServerLocatorWithHA(server0tc, server1tc));
    ClientSessionFactory csf0 = locator.createSessionFactory(server0tc);
    session0 = csf0.createSession(false, true, true);
    ClientSessionFactory csf1 = locator.createSessionFactory(server1tc);
    session1 = csf1.createSession(false, true, true);
    ClientProducer prod0 = session0.createProducer(testAddress);
    ClientConsumer cons1 = session1.createConsumer(queueName);
    session1.start();
    // Now we will simulate a failure of the bridge connection between server0 and server1
    Bridge bridge = server0.getClusterManager().getBridges().get(bridgeName);
    assertNotNull(bridge);
    RemotingConnection forwardingConnection = getForwardingConnection(bridge);
    InVMConnector.failOnCreateConnection = true;
    InVMConnector.numberOfFailures = reconnectAttempts - 1;
    forwardingConnection.fail(new ActiveMQNotConnectedException());
    forwardingConnection = getForwardingConnection(bridge);
    forwardingConnection.fail(new ActiveMQNotConnectedException());
    final ManagementService managementService = server0.getManagementService();
    QueueControl coreQueueControl = (QueueControl) managementService.getResource(ResourceNames.CORE_QUEUE + queueName);
    assertEquals(0, coreQueueControl.getDeliveringCount());
    final int numMessages = NUM_MESSAGES;
    SimpleString propKey = new SimpleString("propkey");
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session0.createMessage(false);
        message.putIntProperty(propKey, i);
        prod0.send(message);
    }
    for (int i = 0; i < numMessages; i++) {
        ClientMessage r1 = cons1.receive(1500);
        assertNotNull(r1);
        assertEquals(i, r1.getObjectProperty(propKey));
    }
    closeServers();
    assertNoMoreConnections();
}

2. BridgeReconnectTest#testFailoverThenFailAgainAndReconnect()

View license
@Test
public void testFailoverThenFailAgainAndReconnect() throws Exception {
    server0 = createActiveMQServer(0, isNetty(), server0Params);
    TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params, "server0tc");
    server0.getConfiguration().setConnectorConfigurations(connectors);
    BridgeConfiguration bridgeConfiguration = createBridgeConfig();
    List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
    bridgeConfigs.add(bridgeConfiguration);
    server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
    CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName);
    List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
    queueConfigs0.add(queueConfig0);
    server0.getConfiguration().setQueueConfigurations(queueConfigs0);
    CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName);
    List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
    queueConfigs1.add(queueConfig1);
    server1.getConfiguration().setQueueConfigurations(queueConfigs1);
    startServers();
    locator = addServerLocator(ActiveMQClient.createServerLocatorWithHA(server0tc, server1tc));
    ClientSessionFactory csf0 = locator.createSessionFactory(server0tc);
    session0 = csf0.createSession(false, true, true);
    ClientSessionFactory csf1 = locator.createSessionFactory(server1tc);
    session1 = csf1.createSession(false, true, true);
    ClientProducer prod0 = session0.createProducer(testAddress);
    ClientConsumer cons1 = session1.createConsumer(queueName);
    session1.start();
    Bridge bridge = server0.getClusterManager().getBridges().get(bridgeName);
    RemotingConnection forwardingConnection = getForwardingConnection(bridge);
    InVMConnector.failOnCreateConnection = true;
    InVMConnector.numberOfFailures = reconnectAttempts - 1;
    forwardingConnection.fail(new ActiveMQNotConnectedException());
    final int numMessages = NUM_MESSAGES;
    SimpleString propKey = new SimpleString("propkey");
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session0.createMessage(false);
        message.putIntProperty(propKey, i);
        prod0.send(message);
    }
    int outOfOrder = -1;
    int supposed = -1;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage r1 = cons1.receive(1500);
        assertNotNull(r1);
        if (outOfOrder == -1 && i != r1.getIntProperty(propKey).intValue()) {
            outOfOrder = r1.getIntProperty(propKey).intValue();
            supposed = i;
        }
    }
    if (outOfOrder != -1) {
        fail("Message " + outOfOrder + " was received out of order, it was supposed to be " + supposed);
    }
    log.info("=========== second failure, sending message");
    // Fail again - should reconnect
    forwardingConnection = ((BridgeImpl) bridge).getForwardingConnection();
    InVMConnector.failOnCreateConnection = true;
    InVMConnector.numberOfFailures = reconnectAttempts - 1;
    forwardingConnection.fail(new ActiveMQException(ActiveMQExceptionType.UNBLOCKED));
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session0.createMessage(false);
        message.putIntProperty(propKey, i);
        prod0.send(message);
    }
    for (int i = 0; i < numMessages; i++) {
        ClientMessage r1 = cons1.receive(1500);
        assertNotNull("Didn't receive message", r1);
        if (outOfOrder == -1 && i != r1.getIntProperty(propKey).intValue()) {
            outOfOrder = r1.getIntProperty(propKey).intValue();
            supposed = i;
        }
    }
    if (outOfOrder != -1) {
        fail("Message " + outOfOrder + " was received out of order, it was supposed to be " + supposed);
    }
    closeServers();
    assertNoMoreConnections();
}

3. BridgeReconnectTest#testDeliveringCountOnBridgeConnectionFailure()

View license
@Test
public void testDeliveringCountOnBridgeConnectionFailure() throws Exception {
    server0 = createActiveMQServer(0, isNetty(), server0Params);
    TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params, "server0tc");
    server0.getConfiguration().setConnectorConfigurations(connectors);
    BridgeConfiguration bridgeConfiguration = createBridgeConfig();
    List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
    bridgeConfigs.add(bridgeConfiguration);
    server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
    CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName);
    List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
    queueConfigs0.add(queueConfig0);
    server0.getConfiguration().setQueueConfigurations(queueConfigs0);
    CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName);
    List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
    queueConfigs1.add(queueConfig1);
    server1.getConfiguration().setQueueConfigurations(queueConfigs1);
    startServers();
    locator = addServerLocator(ActiveMQClient.createServerLocatorWithHA(server0tc, server1tc));
    ClientSessionFactory csf0 = locator.createSessionFactory(server0tc);
    session0 = csf0.createSession(false, true, true);
    ClientSessionFactory csf1 = locator.createSessionFactory(server1tc);
    session1 = csf1.createSession(false, true, true);
    ClientProducer prod0 = session0.createProducer(testAddress);
    session1.start();
    Bridge bridge = server0.getClusterManager().getBridges().get(bridgeName);
    RemotingConnection forwardingConnection = getForwardingConnection(bridge);
    InVMConnector.failOnCreateConnection = true;
    InVMConnector.numberOfFailures = reconnectAttempts - 1;
    //forwardingConnection.fail(new ActiveMQNotConnectedException());
    final int numMessages = NUM_MESSAGES;
    SimpleString propKey = new SimpleString("propkey");
    final Queue queue = (Queue) server0.getPostOffice().getBinding(new SimpleString(queueName)).getBindable();
    System.out.println("DeliveringCount: " + queue.getDeliveringCount());
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session0.createMessage(false);
        message.putIntProperty(propKey, i);
        prod0.send(message);
        if (i == 50) {
            forwardingConnection.fail(new ActiveMQException(ActiveMQExceptionType.UNBLOCKED));
        }
    }
    for (int i = 0; i < 100 && queue.getDeliveringCount() != 0; i++) {
        Thread.sleep(10);
    }
    System.out.println("Check.. DeliveringCount: " + queue.getDeliveringCount());
    assertEquals("Delivering count of a source queue should be zero on connection failure", 0, queue.getDeliveringCount());
    closeServers();
    assertNoMoreConnections();
}

4. BridgeTest#testInjectedTransformer()

Project: activemq-artemis
Source File: BridgeTest.java
View license
@Test
public void testInjectedTransformer() throws Exception {
    final SimpleString ADDRESS = new SimpleString("myAddress");
    final SimpleString QUEUE = new SimpleString("myQueue");
    final String BRIDGE = "myBridge";
    ServiceRegistryImpl serviceRegistry = new ServiceRegistryImpl();
    Transformer transformer = new Transformer() {

        @Override
        public ServerMessage transform(ServerMessage message) {
            return null;
        }
    };
    serviceRegistry.addBridgeTransformer(BRIDGE, transformer);
    Configuration config = createDefaultInVMConfig().addConnectorConfiguration("in-vm", new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    ActiveMQServer server = addServer(new ActiveMQServerImpl(config, null, null, null, serviceRegistry));
    server.start();
    server.waitForActivation(100, TimeUnit.MILLISECONDS);
    server.deployQueue(ADDRESS, QUEUE, null, false, false);
    List<String> connectors = new ArrayList<>();
    connectors.add("in-vm");
    server.deployBridge(new BridgeConfiguration().setName(BRIDGE).setQueueName(QUEUE.toString()).setForwardingAddress(ADDRESS.toString()).setStaticConnectors(connectors));
    Bridge bridge = server.getClusterManager().getBridges().get(BRIDGE);
    assertNotNull(bridge);
    assertEquals(transformer, ((BridgeImpl) bridge).getTransformer());
}