
com.canoo.dp.impl.server.event.DistributedEventBus Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dolphin-platform-distributed-eventbus Show documentation
Show all versions of dolphin-platform-distributed-eventbus Show documentation
The Dolphin Platform is a framework that implements the presentation model pattern and provides amodern way to create enterprise applications. The Platform provides several client implementations that all canbe used in combination with a general sever API.
The newest version!
/*
* Copyright 2015-2018 Canoo Engineering AG.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.canoo.dp.impl.server.event;
import com.canoo.dp.impl.platform.core.Assert;
import com.canoo.platform.core.functional.Subscription;
import com.canoo.platform.remoting.server.event.MessageEventContext;
import com.canoo.platform.remoting.server.event.MessageListener;
import com.canoo.platform.remoting.server.event.Topic;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ITopic;
import org.apiguardian.api.API;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Predicate;
import static org.apiguardian.api.API.Status.INTERNAL;
@API(since = "0.x", status = INTERNAL)
public class DistributedEventBus extends AbstractEventBus {
private final HazelcastInstance hazelcastClient;
private final Map iTopicRegistrations = new ConcurrentHashMap<>();
private final Map iTopicCount = new ConcurrentHashMap<>();
private final Lock hazelcastEventPipeLock = new ReentrantLock();
public DistributedEventBus(final HazelcastInstance hazelcastClient) {
this.hazelcastClient = Assert.requireNonNull(hazelcastClient, "hazelcastClient");
}
protected void publishForOtherSessions(final DolphinEvent event) {
Assert.requireNonNull(event, "event");
final ITopic> topic = toHazelcastTopic(event.getMessageEventContext().getTopic());
topic.publish(event);
}
@Override
public Subscription subscribe(final Topic topic, final MessageListener super T> handler, final Predicate> filter) {
final Subscription basicSubscription = super.subscribe(topic, handler, filter);
final Subscription hazelcastSubscription = createHazelcastSubscription(topic);
return new Subscription() {
@Override
public void unsubscribe() {
hazelcastSubscription.unsubscribe();
basicSubscription.unsubscribe();
}
};
}
private Subscription createHazelcastSubscription(final Topic topic) {
hazelcastEventPipeLock.lock();
try {
final ITopic> hazelcastTopic = toHazelcastTopic(topic);
Assert.requireNonNull(hazelcastTopic, "hazelcastTopic");
final Integer currentCount = iTopicCount.get(topic.getName());
if (currentCount == null || currentCount == 0) {
registerHazelcastEventPipe(hazelcastTopic);
} else {
iTopicCount.put(topic.getName(), currentCount + 1);
}
return new Subscription() {
@Override
public void unsubscribe() {
final Integer currentCount = iTopicCount.get(topic.getName());
if (currentCount > 1) {
iTopicCount.put(topic.getName(), currentCount - 1);
} else {
unregisterHazelcastEventPipe(hazelcastTopic);
}
}
};
} finally {
hazelcastEventPipeLock.unlock();
}
}
private void registerHazelcastEventPipe(final ITopic> topic) {
hazelcastEventPipeLock.lock();
try {
Assert.requireNonNull(topic, "hazelcastTopic");
final String registrationId = topic.addMessageListener(new com.hazelcast.core.MessageListener>() {
@Override
public void onMessage(com.hazelcast.core.Message> message) {
final DolphinEvent event = message.getMessageObject();
triggerEventHandling(event);
}
});
Assert.requireNonBlank(registrationId, "registrationId");
iTopicRegistrations.put(topic.getName(), registrationId);
iTopicCount.put(topic.getName(), 1);
} finally {
hazelcastEventPipeLock.unlock();
}
}
private void unregisterHazelcastEventPipe(final ITopic> topic) {
hazelcastEventPipeLock.lock();
try {
Assert.requireNonNull(topic, "hazelcastTopic");
final Integer count = iTopicCount.get(topic.getName());
if (count == null || count != 1) {
throw new IllegalStateException("Count for topic " + topic.getName() + " is wrong: " + count);
}
final String registrationId = iTopicRegistrations.get(topic.getName());
Assert.requireNonBlank(registrationId, "registrationId");
topic.removeMessageListener(registrationId);
iTopicRegistrations.remove(topic.getName());
iTopicCount.remove(topic.getName());
} finally {
hazelcastEventPipeLock.unlock();
}
}
private ITopic> toHazelcastTopic(final Topic topic) {
return hazelcastClient.getTopic(topic.getName());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy