use of com.hazelcast.topic.ITopic in project hazelcast by hazelcast.
the class ReliableTopicCreateTest method testConfiguredListenerClassAndHazelcastInstanceAware.
@Test
public void testConfiguredListenerClassAndHazelcastInstanceAware() {
Config config = new Config();
config.addReliableTopicConfig(new ReliableTopicConfig("foo*").addMessageListenerConfig(new ListenerConfig(InstanceAwareReliableMessageListenerMock.class.getName())));
HazelcastInstance hz = createHazelcastInstance(config);
ITopic topic = hz.getReliableTopic("foo");
ReliableTopicProxy proxy = assertInstanceOf(ReliableTopicProxy.class, topic);
// check there is one listener.
assertEquals(1, proxy.runnersMap.size());
// check that the listener is of the right class.
MessageRunner runner = (MessageRunner) proxy.runnersMap.values().iterator().next();
InstanceAwareReliableMessageListenerMock mock = assertInstanceOf(InstanceAwareReliableMessageListenerMock.class, runner.listener);
assertNotNull(mock.hz);
}
use of com.hazelcast.topic.ITopic in project hazelcast by hazelcast.
the class ClientReliableTopicOnClusterRestartTest method shouldContinue_OnClusterRestart_afterInvocationTimeout.
@Test
public void shouldContinue_OnClusterRestart_afterInvocationTimeout() throws InterruptedException {
HazelcastInstance member = hazelcastFactory.newHazelcastInstance(smallInstanceConfig());
ClientConfig clientConfig = new ClientConfig();
clientConfig.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
int invocationTimeoutSeconds = 2;
clientConfig.setProperty(ClientProperty.INVOCATION_TIMEOUT_SECONDS.getName(), String.valueOf(invocationTimeoutSeconds));
HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
final CountDownLatch messageArrived = new CountDownLatch(1);
String topicName = "topic";
ITopic<String> topic = client.getReliableTopic(topicName);
UUID registrationId = topic.addMessageListener(createListener(true, m -> messageArrived.countDown()));
member.shutdown();
// wait for the topic operation to timeout
Thread.sleep(TimeUnit.SECONDS.toMillis(invocationTimeoutSeconds));
member = hazelcastFactory.newHazelcastInstance(smallInstanceConfig());
member.getReliableTopic(topicName).publish("message");
assertOpenEventually(messageArrived);
ClientReliableTopicProxy<?> proxy = (ClientReliableTopicProxy<?>) topic;
assertFalse(proxy.isListenerCancelled(registrationId));
}
use of com.hazelcast.topic.ITopic in project hazelcast by hazelcast.
the class ClientReliableTopicOnClusterRestartTest method shouldFail_OnClusterRestart_whenDataLoss_notLossTolerant.
@Test
public void shouldFail_OnClusterRestart_whenDataLoss_notLossTolerant() {
Config config = smallInstanceConfig();
String topicName = "topic";
config.getRingbufferConfig(topicName).setCapacity(10);
HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config);
ClientConfig clientConfig = new ClientConfig();
clientConfig.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
AtomicLong messageCount = new AtomicLong();
// initialises listener seq up to 10000
for (int i = 0; i < 10_000; i++) {
member.getReliableTopic(topicName).publish("message");
}
ITopic<String> topic = client.getReliableTopic(topicName);
UUID registrationId = topic.addMessageListener(createListener(false, m -> messageCount.incrementAndGet()));
member.shutdown();
member = hazelcastFactory.newHazelcastInstance(config);
HazelcastInstance finalMember = member;
assertTrueEventually(() -> {
// we require at least one new message to detect that the ringbuffer was recreated
finalMember.getReliableTopic(topicName).publish("message");
ClientReliableTopicProxy<?> proxy = (ClientReliableTopicProxy<?>) topic;
assertTrue(proxy.isListenerCancelled(registrationId));
});
assertEquals(0, messageCount.get());
}
use of com.hazelcast.topic.ITopic in project hazelcast by hazelcast.
the class ClientReliableTopicTest method testListener.
@Test
public void testListener() throws InterruptedException {
ITopic topic = client.getReliableTopic(randomString());
int messageCount = 10;
final CountDownLatch latch = new CountDownLatch(messageCount);
MessageListener listener = new MessageListener() {
public void onMessage(Message message) {
latch.countDown();
}
};
topic.addMessageListener(listener);
for (int i = 0; i < messageCount; i++) {
topic.publish(i);
}
assertTrue(latch.await(20, TimeUnit.SECONDS));
}
use of com.hazelcast.topic.ITopic in project hazelcast by hazelcast.
the class ClientReliableTopicTest method testAlwaysStartAfterTail.
// makes sure that when a listener is register, we don't see any messages being published before
// it got registered. We'll only see the messages after it got registered.
@Test
public void testAlwaysStartAfterTail() {
final ITopic topic = client.getReliableTopic(randomString());
topic.publish("1");
topic.publish("2");
topic.publish("3");
spawn(new Runnable() {
@Override
public void run() {
sleepSeconds(5);
topic.publish("4");
topic.publish("5");
topic.publish("6");
}
});
final ReliableMessageListenerMock listener = new ReliableMessageListenerMock();
topic.addMessageListener(listener);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(asList("4", "5", "6"), Arrays.asList(listener.objects.toArray()));
}
});
}
Aggregations