use of org.eclipse.scout.rt.mom.api.ISubscription in project scout.rt by eclipse.
the class ClusterSynchronizationService method disable.
@Override
public boolean disable() {
if (!isEnabled()) {
return true;
}
final ISubscription subscription;
synchronized (m_subscriptionLock) {
subscription = m_subscription;
m_subscription = null;
}
try {
if (subscription != null) {
subscription.dispose();
}
} catch (RuntimeException e) {
LOG.error("Failed to unsubscribe from {}", IClusterMomDestinations.CLUSTER_NOTIFICATION_TOPIC, e);
}
return true;
}
use of org.eclipse.scout.rt.mom.api.ISubscription in project scout.rt by eclipse.
the class JmsMomImplementorTest method testSubscriptionDisposeSynchronized.
/**
* In case of single threaded subscription, {@link ISubscription#dispose()} waits for any ongoing processing of this
* subscription to finish.
*/
private void testSubscriptionDisposeSynchronized(SubscribeInput subscribeInput) throws InterruptedException {
final Capturer<String> capturer = new Capturer<>();
final CountDownLatch latch = new CountDownLatch(1);
IDestination<String> queue = MOM.newDestination("test/mom/testPublishText", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
m_disposables.add(MOM.registerMarshaller(JmsTestMom.class, queue, BEANS.get(TextMarshaller.class)));
MOM.publish(JmsTestMom.class, queue, "hello world");
final ISubscription subscription = MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {
@Override
public void onMessage(IMessage<String> message) {
capturer.set(message.getTransferObject());
try {
latch.await();
} catch (InterruptedException e) {
throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
}
}
}, subscribeInput);
assertEquals("hello world", capturer.get());
IFuture<Void> disposeFuture = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
subscription.dispose();
}
}, Jobs.newInput().withName("dispose subscription").withExecutionHint(m_testJobExecutionHint).withExceptionHandling(null, false));
// Verify
try {
disposeFuture.awaitDoneAndGet(200, TimeUnit.MILLISECONDS);
assertEquals(SubscribeInput.ACKNOWLEDGE_AUTO, subscribeInput.getAcknowledgementMode());
} catch (TimedOutError e) {
assertEquals(SubscribeInput.ACKNOWLEDGE_AUTO_SINGLE_THREADED, subscribeInput.getAcknowledgementMode());
} finally {
latch.countDown();
}
}
use of org.eclipse.scout.rt.mom.api.ISubscription in project scout.rt by eclipse.
the class JmsMomImplementorTest method testTopicDurableSubscription.
@Test
public void testTopicDurableSubscription() throws InterruptedException {
if (m_parameter.getImplementor() == J2eeJmsMomImplementor.class) {
// J2EE implementor does not set any client id; therefore no durable subscription is possible
return;
}
final IDestination<String> topic = MOM.newDestination("test/mom/testTopicPublishSubscribe", DestinationType.TOPIC, ResolveMethod.DEFINE, null);
final String durableSubscriptionName = "Durable-Test-Subscription";
// 1. Subscribe (non-durable)
final Capturer<String> capturer1 = new Capturer<>();
final ISubscription subscription1 = MOM.subscribe(JmsTestMom.class, topic, new CapturerListener<>(capturer1), MOM.newSubscribeInput());
m_disposables.add(subscription1);
// 2. Disconnect
subscription1.dispose();
// 3. Publish a message
MOM.publish(JmsTestMom.class, topic, "lost message");
// no one is listening
capturer1.assertEmpty(1, TimeUnit.SECONDS);
// 4. Subscribe again (durable)
final Capturer<String> capturer2 = new Capturer<>();
final ISubscription subscription2 = MOM.subscribe(JmsTestMom.class, topic, new CapturerListener<>(capturer2), MOM.newSubscribeInput().withDurableSubscription(durableSubscriptionName));
m_disposables.add(subscription2);
// 5. Assert that message is lost
capturer2.assertEmpty(1, TimeUnit.SECONDS);
// 6. Disconnect
subscription2.dispose();
// 7. Publish an other message
MOM.publish(JmsTestMom.class, topic, "hello world");
// not yet
capturer2.assertEmpty(1, TimeUnit.SECONDS);
// 8. Subscribe again (durable, same name)
final Capturer<String> capturer3 = new Capturer<>();
final ISubscription subscription3 = MOM.subscribe(JmsTestMom.class, topic, new CapturerListener<>(capturer3), MOM.newSubscribeInput().withDurableSubscription(durableSubscriptionName));
m_disposables.add(subscription3);
// 9. Assert that the message is received
assertEquals("hello world", capturer3.get(1, TimeUnit.SECONDS));
// 10. Disconnect and cancel the durable subscription
subscription3.dispose();
MOM.cancelDurableSubscription(JmsTestMom.class, durableSubscriptionName);
// 11. Publish another message
MOM.publish(JmsTestMom.class, topic, "hello universe");
// still the same old message
assertEquals("hello world", capturer3.get(1, TimeUnit.SECONDS));
// 12. Subscribe again (durable, same name)
final Capturer<String> capturer4 = new Capturer<>();
final ISubscription subscription4 = MOM.subscribe(JmsTestMom.class, topic, new CapturerListener<>(capturer4), MOM.newSubscribeInput().withDurableSubscription(durableSubscriptionName));
m_disposables.add(subscription4);
// 13. Assert that message is still lost, even if the same name was used (because the previous subscription was cancelled explicitly)
capturer4.assertEmpty(1, TimeUnit.SECONDS);
}
Aggregations