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);
}
}
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);
}
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) {
}
}
}
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();
}
}
}
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);
}
}
Aggregations