Search in sources :

Example 1 with BroadcastEndpoint

use of org.apache.activemq.artemis.api.core.BroadcastEndpoint in project activemq-artemis by apache.

the class DiscoveryTest method testJGropusChannelReferenceCounting1.

/**
 * Create one broadcaster and 50 receivers. Make sure broadcasting works.
 * Then stop all of the receivers, and create 50 new ones. Make sure the
 * 50 new ones are receiving data from the broadcasting.
 *
 * @throws Exception
 */
@Test
public void testJGropusChannelReferenceCounting1() throws Exception {
    BroadcastEndpointFactory factory = new JGroupsFileBroadcastEndpointFactory().setChannelName("tst").setFile(TEST_JGROUPS_CONF_FILE);
    BroadcastEndpoint broadcaster = factory.createBroadcastEndpoint();
    broadcaster.openBroadcaster();
    int num = 50;
    BroadcastEndpoint[] receivers = new BroadcastEndpoint[num];
    for (int i = 0; i < num; i++) {
        receivers[i] = factory.createBroadcastEndpoint();
        receivers[i].openClient();
    }
    try {
        final byte[] data = new byte[] { 1, 2, 3, 4, 5 };
        broadcaster.broadcast(data);
        for (int i = 0; i < num; i++) {
            byte[] received = receivers[i].receiveBroadcast(5000, TimeUnit.MILLISECONDS);
            assertNotNull(received);
            assertEquals(5, received.length);
            assertEquals(1, received[0]);
            assertEquals(2, received[1]);
            assertEquals(3, received[2]);
            assertEquals(4, received[3]);
            assertEquals(5, received[4]);
        }
        for (int i = 0; i < num; i++) {
            receivers[i].close(false);
        }
        // new ones
        for (int i = 0; i < num; i++) {
            receivers[i] = factory.createBroadcastEndpoint();
            receivers[i].openClient();
        }
        broadcaster.broadcast(data);
        for (int i = 0; i < num; i++) {
            byte[] received = receivers[i].receiveBroadcast(5000, TimeUnit.MILLISECONDS);
            assertNotNull(received);
            assertEquals(5, received.length);
            assertEquals(1, received[0]);
            assertEquals(2, received[1]);
            assertEquals(3, received[2]);
            assertEquals(4, received[3]);
            assertEquals(5, received[4]);
        }
    } finally {
        for (int i = 0; i < num; i++) {
            receivers[i].close(false);
        }
        broadcaster.close(true);
    }
}
Also used : BroadcastEndpoint(org.apache.activemq.artemis.api.core.BroadcastEndpoint) JGroupsFileBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.JGroupsFileBroadcastEndpointFactory) BroadcastEndpoint(org.apache.activemq.artemis.api.core.BroadcastEndpoint) JGroupsFileBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.JGroupsFileBroadcastEndpointFactory) UDPBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.UDPBroadcastEndpointFactory) BroadcastEndpointFactory(org.apache.activemq.artemis.api.core.BroadcastEndpointFactory) Test(org.junit.Test)

Example 2 with BroadcastEndpoint

use of org.apache.activemq.artemis.api.core.BroadcastEndpoint in project activemq-artemis by apache.

the class DiscoveryStayAliveTest method sendBadData.

private static void sendBadData(BroadcastEndpointFactory factoryEndpoint) throws Exception {
    BroadcastEndpoint endpoint = factoryEndpoint.createBroadcastEndpoint();
    ActiveMQBuffer buffer = ActiveMQBuffers.dynamicBuffer(500);
    buffer.writeString("This is a test1!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    buffer.writeString("This is a test2!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    byte[] bytes = new byte[buffer.writerIndex()];
    buffer.readBytes(bytes);
    // messing up with the string!!!
    for (int i = bytes.length - 10; i < bytes.length; i++) {
        bytes[i] = 0;
    }
    endpoint.openBroadcaster();
    endpoint.broadcast(bytes);
    endpoint.close(true);
}
Also used : BroadcastEndpoint(org.apache.activemq.artemis.api.core.BroadcastEndpoint) BroadcastEndpoint(org.apache.activemq.artemis.api.core.BroadcastEndpoint) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer)

Example 3 with BroadcastEndpoint

use of org.apache.activemq.artemis.api.core.BroadcastEndpoint in project activemq-artemis by apache.

the class JGroupsBroadcastTest method testRefCount.

@Test
public void testRefCount() throws Exception {
    JChannel channel = null;
    JChannel newChannel = null;
    try {
        PlainConfigurator configurator = new PlainConfigurator(jgroupsConfigString);
        channel = new JChannel(configurator);
        String channelName1 = "channel1";
        BroadcastEndpointFactory jgroupsBroadcastCfg1 = new ChannelBroadcastEndpointFactory(channel, channelName1);
        BroadcastEndpoint channelEndpoint1 = jgroupsBroadcastCfg1.createBroadcastEndpoint();
        BroadcastEndpoint channelEndpoint2 = jgroupsBroadcastCfg1.createBroadcastEndpoint();
        BroadcastEndpoint channelEndpoint3 = jgroupsBroadcastCfg1.createBroadcastEndpoint();
        channelEndpoint1.close(true);
        Assert.assertTrue(channel.isOpen());
        channelEndpoint2.close(true);
        Assert.assertTrue(channel.isOpen());
        channelEndpoint3.close(true);
        Assert.assertTrue(channel.isOpen());
        channel.close();
        // channel wrapper is recreated
        try {
            channelEndpoint2.openClient();
            Assert.fail("this should be closed");
        } catch (Exception e) {
        }
        newChannel = new JChannel(configurator);
        jgroupsBroadcastCfg1 = new ChannelBroadcastEndpointFactory(newChannel, channelName1);
        channelEndpoint1 = jgroupsBroadcastCfg1.createBroadcastEndpoint();
        channelEndpoint1.openClient();
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        try {
            channel.close();
        } catch (Throwable ignored) {
        }
        try {
            newChannel.close();
        } catch (Throwable ignored) {
        }
    }
}
Also used : ChannelBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.ChannelBroadcastEndpointFactory) BroadcastEndpoint(org.apache.activemq.artemis.api.core.BroadcastEndpoint) JChannel(org.jgroups.JChannel) PlainConfigurator(org.jgroups.conf.PlainConfigurator) BroadcastEndpointFactory(org.apache.activemq.artemis.api.core.BroadcastEndpointFactory) ChannelBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.ChannelBroadcastEndpointFactory) Test(org.junit.Test)

Example 4 with BroadcastEndpoint

use of org.apache.activemq.artemis.api.core.BroadcastEndpoint in project activemq-artemis by apache.

the class DiscoveryTest method testStraightSendReceiveJGroups.

@Test
public void testStraightSendReceiveJGroups() throws Exception {
    BroadcastEndpoint broadcaster = null;
    BroadcastEndpoint client = null;
    try {
        JGroupsFileBroadcastEndpointFactory endpointFactory = new JGroupsFileBroadcastEndpointFactory().setChannelName("tst").setFile(TEST_JGROUPS_CONF_FILE);
        broadcaster = endpointFactory.createBroadcastEndpoint();
        broadcaster.openBroadcaster();
        client = endpointFactory.createBroadcastEndpoint();
        client.openClient();
        Thread.sleep(1000);
        byte[] randomBytes = "PQP".getBytes();
        broadcaster.broadcast(randomBytes);
        byte[] btreceived = client.receiveBroadcast(5, TimeUnit.SECONDS);
        System.out.println("BTReceived = " + Arrays.toString(btreceived));
        assertNotNull(btreceived);
        assertEquals(randomBytes.length, btreceived.length);
        for (int i = 0; i < randomBytes.length; i++) {
            assertEquals(randomBytes[i], btreceived[i]);
        }
    } finally {
        try {
            if (broadcaster != null)
                broadcaster.close(true);
            if (client != null)
                client.close(false);
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }
}
Also used : BroadcastEndpoint(org.apache.activemq.artemis.api.core.BroadcastEndpoint) JGroupsFileBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.JGroupsFileBroadcastEndpointFactory) BroadcastEndpoint(org.apache.activemq.artemis.api.core.BroadcastEndpoint) Test(org.junit.Test)

Example 5 with BroadcastEndpoint

use of org.apache.activemq.artemis.api.core.BroadcastEndpoint in project activemq-artemis by apache.

the class DiscoveryTest method testJGropusChannelReferenceCounting.

/**
 * Create one broadcaster and 100 receivers. Make sure broadcasting works.
 * Then stop 99 of the receivers, the last one could still be working.
 *
 * @throws Exception
 */
@Test
public void testJGropusChannelReferenceCounting() throws Exception {
    BroadcastEndpointFactory factory = new JGroupsFileBroadcastEndpointFactory().setChannelName("tst").setFile(TEST_JGROUPS_CONF_FILE);
    BroadcastEndpoint broadcaster = factory.createBroadcastEndpoint();
    broadcaster.openBroadcaster();
    try {
        int num = 100;
        BroadcastEndpoint[] receivers = new BroadcastEndpoint[num];
        for (int i = 0; i < num; i++) {
            receivers[i] = factory.createBroadcastEndpoint();
            receivers[i].openClient();
        }
        final byte[] data = new byte[] { 1, 2, 3, 4, 5 };
        broadcaster.broadcast(data);
        for (int i = 0; i < num; i++) {
            byte[] received = receivers[i].receiveBroadcast(5000, TimeUnit.MILLISECONDS);
            assertNotNull(received);
            assertEquals(5, received.length);
            assertEquals(1, received[0]);
            assertEquals(2, received[1]);
            assertEquals(3, received[2]);
            assertEquals(4, received[3]);
            assertEquals(5, received[4]);
        }
        for (int i = 0; i < num - 1; i++) {
            receivers[i].close(false);
        }
        byte[] data1 = receivers[num - 1].receiveBroadcast(5, TimeUnit.SECONDS);
        assertNull(data1);
        broadcaster.broadcast(data);
        data1 = receivers[num - 1].receiveBroadcast(5, TimeUnit.SECONDS);
        assertNotNull(data1);
        assertEquals(5, data1.length);
        assertEquals(1, data1[0]);
        assertEquals(2, data1[1]);
        assertEquals(3, data1[2]);
        assertEquals(4, data1[3]);
        assertEquals(5, data1[4]);
        receivers[num - 1].close(false);
    } finally {
        broadcaster.close(true);
    }
}
Also used : BroadcastEndpoint(org.apache.activemq.artemis.api.core.BroadcastEndpoint) JGroupsFileBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.JGroupsFileBroadcastEndpointFactory) BroadcastEndpoint(org.apache.activemq.artemis.api.core.BroadcastEndpoint) JGroupsFileBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.JGroupsFileBroadcastEndpointFactory) UDPBroadcastEndpointFactory(org.apache.activemq.artemis.api.core.UDPBroadcastEndpointFactory) BroadcastEndpointFactory(org.apache.activemq.artemis.api.core.BroadcastEndpointFactory) Test(org.junit.Test)

Aggregations

BroadcastEndpoint (org.apache.activemq.artemis.api.core.BroadcastEndpoint)8 Test (org.junit.Test)6 BroadcastEndpointFactory (org.apache.activemq.artemis.api.core.BroadcastEndpointFactory)4 JGroupsFileBroadcastEndpointFactory (org.apache.activemq.artemis.api.core.JGroupsFileBroadcastEndpointFactory)4 UDPBroadcastEndpointFactory (org.apache.activemq.artemis.api.core.UDPBroadcastEndpointFactory)3 Hashtable (java.util.Hashtable)1 Context (javax.naming.Context)1 InitialContext (javax.naming.InitialContext)1 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)1 ChannelBroadcastEndpointFactory (org.apache.activemq.artemis.api.core.ChannelBroadcastEndpointFactory)1 DiscoveryGroupConfiguration (org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration)1 JGroupsFileBroadcastEndpoint (org.apache.activemq.artemis.api.core.JGroupsFileBroadcastEndpoint)1 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)1 ActiveMQInitialContextFactory (org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory)1 JChannel (org.jgroups.JChannel)1 PlainConfigurator (org.jgroups.conf.PlainConfigurator)1 After (org.junit.After)1