INTEXT-150 Extract Hazelcast Headers and Payloads
JIRA: https://jira.spring.io/browse/INTEXT-150 Refactorings are committed. Polishing: * Add generic type to the `AbstractHazelcastEventListener` for cleaner implementors * Remove redundant `Assert`s * Polishing some code style
This commit is contained in:
committed by
Artem Bilan
parent
20d0192fc0
commit
95f7b239f7
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.hazelcast.common;
|
||||
package org.springframework.integration.hazelcast;
|
||||
|
||||
/**
|
||||
* Enumeration of Cache Event Types
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.hazelcast.common;
|
||||
package org.springframework.integration.hazelcast;
|
||||
|
||||
/**
|
||||
* Enumeration of Cache Listening Policy Type
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.hazelcast.common;
|
||||
package org.springframework.integration.hazelcast;
|
||||
|
||||
/**
|
||||
* Enumeration of Distributed SQL Iteration Type
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.integration.hazelcast;
|
||||
|
||||
/**
|
||||
* Hazelcast Message Headers
|
||||
*
|
||||
* @author Eren Avsarogullari
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class HazelcastHeaders {
|
||||
|
||||
private static final String PREFIX = "hazelcast_";
|
||||
|
||||
public static final String EVENT_TYPE = PREFIX + "eventType";
|
||||
|
||||
public static final String MEMBER = PREFIX + "member";
|
||||
|
||||
public static final String CACHE_NAME = PREFIX + "cacheName";
|
||||
|
||||
public static final String PUBLISHING_TIME = PREFIX + "publishingTime";
|
||||
|
||||
}
|
||||
@@ -14,12 +14,15 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.hazelcast.common;
|
||||
package org.springframework.integration.hazelcast;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import reactor.util.CollectionUtils;
|
||||
import reactor.util.StringUtils;
|
||||
|
||||
import com.hazelcast.core.DistributedObject;
|
||||
import com.hazelcast.core.IList;
|
||||
import com.hazelcast.core.IMap;
|
||||
@@ -29,9 +32,6 @@ import com.hazelcast.core.ITopic;
|
||||
import com.hazelcast.core.MultiMap;
|
||||
import com.hazelcast.core.ReplicatedMap;
|
||||
|
||||
import reactor.util.CollectionUtils;
|
||||
import reactor.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Common Validator for Hazelcast Integration. It validates cache types and events.
|
||||
*
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.hazelcast.common;
|
||||
package org.springframework.integration.hazelcast;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
@@ -1,4 +0,0 @@
|
||||
/**
|
||||
* Provides common used types and classes.
|
||||
*/
|
||||
package org.springframework.integration.hazelcast.common;
|
||||
@@ -21,7 +21,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.integration.config.IntegrationConfigurationInitializer;
|
||||
import org.springframework.integration.hazelcast.common.HazelcastLocalInstanceRegistrar;
|
||||
import org.springframework.integration.hazelcast.HazelcastLocalInstanceRegistrar;
|
||||
|
||||
/**
|
||||
* The Hazelcast Integration infrastructure {@code beanFactory} initializer.
|
||||
|
||||
@@ -19,15 +19,19 @@ package org.springframework.integration.hazelcast.inbound;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Collections;
|
||||
import java.util.EventObject;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.integration.endpoint.MessageProducerSupport;
|
||||
import org.springframework.integration.hazelcast.common.CacheEventType;
|
||||
import org.springframework.integration.hazelcast.common.CacheListeningPolicyType;
|
||||
import org.springframework.integration.hazelcast.common.HazelcastIntegrationDefinitionValidator;
|
||||
import org.springframework.integration.hazelcast.common.HazelcastLocalInstanceRegistrar;
|
||||
import org.springframework.integration.hazelcast.CacheEventType;
|
||||
import org.springframework.integration.hazelcast.CacheListeningPolicyType;
|
||||
import org.springframework.integration.hazelcast.HazelcastHeaders;
|
||||
import org.springframework.integration.hazelcast.HazelcastIntegrationDefinitionValidator;
|
||||
import org.springframework.integration.hazelcast.HazelcastLocalInstanceRegistrar;
|
||||
import org.springframework.integration.hazelcast.message.EntryEventMessagePayload;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.hazelcast.core.AbstractIMapEvent;
|
||||
@@ -45,20 +49,21 @@ import reactor.util.StringUtils;
|
||||
* Hazelcast Base Event-Driven Message Producer.
|
||||
*
|
||||
* @author Eren Avsarogullari
|
||||
* @author Artem Bilan
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class AbstractHazelcastMessageProducer extends MessageProducerSupport {
|
||||
|
||||
protected final DistributedObject distributedObject;
|
||||
|
||||
private CacheListeningPolicyType cacheListeningPolicy = CacheListeningPolicyType.SINGLE;
|
||||
private volatile CacheListeningPolicyType cacheListeningPolicy = CacheListeningPolicyType.SINGLE;
|
||||
|
||||
private String hazelcastRegisteredEventListenerId;
|
||||
private volatile String hazelcastRegisteredEventListenerId;
|
||||
|
||||
private Set<String> cacheEvents = Collections.singleton(CacheEventType.ADDED.name());
|
||||
|
||||
protected AbstractHazelcastMessageProducer(DistributedObject distributedObject) {
|
||||
Assert.notNull(distributedObject, "cache must not be null");
|
||||
public AbstractHazelcastMessageProducer(DistributedObject distributedObject) {
|
||||
Assert.notNull(distributedObject, "'distributedObject' must not be null");
|
||||
this.distributedObject = distributedObject;
|
||||
}
|
||||
|
||||
@@ -68,8 +73,8 @@ public abstract class AbstractHazelcastMessageProducer extends MessageProducerSu
|
||||
|
||||
public void setCacheEventTypes(String cacheEventTypes) {
|
||||
HazelcastIntegrationDefinitionValidator.validateEnumType(CacheEventType.class, cacheEventTypes);
|
||||
final Set<String> cacheEvents = StringUtils.commaDelimitedListToSet(cacheEventTypes);
|
||||
Assert.notEmpty(cacheEvents, "cacheEvents must have elements");
|
||||
Set<String> cacheEvents = StringUtils.commaDelimitedListToSet(cacheEventTypes);
|
||||
Assert.notEmpty(cacheEvents, "'cacheEvents' must have elements");
|
||||
HazelcastIntegrationDefinitionValidator.validateCacheEventsByDistributedObject(
|
||||
this.distributedObject, cacheEvents);
|
||||
this.cacheEvents = cacheEvents;
|
||||
@@ -80,7 +85,7 @@ public abstract class AbstractHazelcastMessageProducer extends MessageProducerSu
|
||||
}
|
||||
|
||||
public void setCacheListeningPolicy(CacheListeningPolicyType cacheListeningPolicy) {
|
||||
Assert.notNull(cacheListeningPolicy, "cacheListeningPolicy must not be null");
|
||||
Assert.notNull(cacheListeningPolicy, "'cacheListeningPolicy' must not be null");
|
||||
this.cacheListeningPolicy = cacheListeningPolicy;
|
||||
}
|
||||
|
||||
@@ -92,28 +97,27 @@ public abstract class AbstractHazelcastMessageProducer extends MessageProducerSu
|
||||
this.hazelcastRegisteredEventListenerId = hazelcastRegisteredEventListenerId;
|
||||
}
|
||||
|
||||
protected abstract class AbstractHazelcastEventListener {
|
||||
protected abstract class AbstractHazelcastEventListener<E> {
|
||||
|
||||
protected abstract void processEvent(EventObject event);
|
||||
protected abstract void processEvent(E event);
|
||||
|
||||
protected void sendMessage(final EventObject event, final InetSocketAddress socketAddress,
|
||||
final CacheListeningPolicyType cacheListeningPolicyType) {
|
||||
protected abstract Message<?> toMessage(E event);
|
||||
|
||||
protected void sendMessage(E event, InetSocketAddress socketAddress,
|
||||
CacheListeningPolicyType cacheListeningPolicyType) {
|
||||
if (CacheListeningPolicyType.ALL == cacheListeningPolicyType || isEventAcceptable(socketAddress)) {
|
||||
AbstractHazelcastMessageProducer.this.sendMessage(getMessageBuilderFactory().withPayload(event).build());
|
||||
AbstractHazelcastMessageProducer.this.sendMessage(toMessage(event));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isEventAcceptable(final InetSocketAddress socketAddress) {
|
||||
final Set<HazelcastInstance> hazelcastInstanceSet = Hazelcast.getAllHazelcastInstances();
|
||||
final Set<SocketAddress> localSocketAddressesSet = getLocalSocketAddresses(hazelcastInstanceSet);
|
||||
if ((!localSocketAddressesSet.isEmpty())
|
||||
return (!localSocketAddressesSet.isEmpty())
|
||||
&& (localSocketAddressesSet.contains(socketAddress) ||
|
||||
isEventComingFromNonRegisteredHazelcastInstance(hazelcastInstanceSet.iterator().next(),
|
||||
localSocketAddressesSet, socketAddress))) {
|
||||
return true;
|
||||
}
|
||||
localSocketAddressesSet, socketAddress));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private Set<SocketAddress> getLocalSocketAddresses(final Set<HazelcastInstance> hazelcastInstanceSet) {
|
||||
@@ -140,7 +144,7 @@ public abstract class AbstractHazelcastMessageProducer extends MessageProducerSu
|
||||
}
|
||||
|
||||
protected final class HazelcastEntryListener<K, V> extends
|
||||
AbstractHazelcastEventListener implements EntryListener<K, V> {
|
||||
AbstractHazelcastEventListener<AbstractIMapEvent> implements EntryListener<K, V> {
|
||||
|
||||
@Override
|
||||
public void entryAdded(EntryEvent<K, V> event) {
|
||||
@@ -173,12 +177,9 @@ public abstract class AbstractHazelcastMessageProducer extends MessageProducerSu
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processEvent(EventObject event) {
|
||||
Assert.notNull(event, "event must not be null");
|
||||
|
||||
if (getCacheEvents().contains(((AbstractIMapEvent) event).getEventType().toString())) {
|
||||
sendMessage(event, ((AbstractIMapEvent) event).getMember().getSocketAddress(),
|
||||
getCacheListeningPolicy());
|
||||
protected void processEvent(AbstractIMapEvent event) {
|
||||
if (getCacheEvents().contains(event.getEventType().toString())) {
|
||||
sendMessage(event, event.getMember().getSocketAddress(), getCacheListeningPolicy());
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
@@ -186,6 +187,29 @@ public abstract class AbstractHazelcastMessageProducer extends MessageProducerSu
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Message<?> toMessage(AbstractIMapEvent event) {
|
||||
final Map<String, Object> headers = new HashMap<String, Object>();
|
||||
headers.put(HazelcastHeaders.EVENT_TYPE, event.getEventType().name());
|
||||
headers.put(HazelcastHeaders.MEMBER, event.getMember().getSocketAddress());
|
||||
headers.put(HazelcastHeaders.CACHE_NAME, event.getName());
|
||||
|
||||
if (event instanceof EntryEvent) {
|
||||
EntryEvent<K, V> entryEvent = (EntryEvent<K, V>) event;
|
||||
EntryEventMessagePayload<K, V> messagePayload = new EntryEventMessagePayload<>(entryEvent.getKey(),
|
||||
entryEvent.getValue(), entryEvent.getOldValue());
|
||||
return getMessageBuilderFactory().withPayload(messagePayload).copyHeaders(headers).build();
|
||||
}
|
||||
else if (event instanceof MapEvent) {
|
||||
return getMessageBuilderFactory()
|
||||
.withPayload(((MapEvent) event).getNumberOfEntriesAffected()).copyHeaders(headers).build();
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Invalid event is received. Event : " + event);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class HazelcastContinuousQueryMessageProducer extends AbstractHazelcastMe
|
||||
@SuppressWarnings("rawtypes")
|
||||
public HazelcastContinuousQueryMessageProducer(IMap distributedMap, String predicate) {
|
||||
super(distributedMap);
|
||||
Assert.hasText(predicate, "predicate must not be null");
|
||||
Assert.hasText(predicate, "'predicate' must not be null");
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.integration.endpoint.AbstractMessageSource;
|
||||
import org.springframework.integration.hazelcast.common.DistributedSQLIterationType;
|
||||
import org.springframework.integration.hazelcast.DistributedSQLIterationType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
@@ -39,19 +39,19 @@ public class HazelcastDistributedSQLMessageSource extends AbstractMessageSource
|
||||
|
||||
private final IMap<?, ?> distributedMap;
|
||||
|
||||
private final String distributedSQL;
|
||||
private final String distributedSql;
|
||||
|
||||
private DistributedSQLIterationType iterationType = DistributedSQLIterationType.VALUE;
|
||||
|
||||
public HazelcastDistributedSQLMessageSource(IMap distributedMap, String distributedSQL) {
|
||||
Assert.notNull(distributedMap, "cache must not be null");
|
||||
Assert.hasText(distributedSQL, "distributed-sql must not be null");
|
||||
public HazelcastDistributedSQLMessageSource(IMap distributedMap, String distributedSql) {
|
||||
Assert.notNull(distributedMap, "'distributedMap' must not be null");
|
||||
Assert.hasText(distributedSql, "'distributedSql' must not be empty");
|
||||
this.distributedMap = distributedMap;
|
||||
this.distributedSQL = distributedSQL;
|
||||
this.distributedSql = distributedSql;
|
||||
}
|
||||
|
||||
public void setIterationType(DistributedSQLIterationType iterationType) {
|
||||
Assert.notNull(this.iterationType, "iterationType must not be null");
|
||||
Assert.notNull(this.iterationType, "'iterationType' must not be null");
|
||||
this.iterationType = iterationType;
|
||||
}
|
||||
|
||||
@@ -65,18 +65,18 @@ public class HazelcastDistributedSQLMessageSource extends AbstractMessageSource
|
||||
switch (this.iterationType) {
|
||||
case ENTRY:
|
||||
return getDistributedSQLResultSet(Collections
|
||||
.unmodifiableCollection(this.distributedMap.entrySet(new SqlPredicate(this.distributedSQL))));
|
||||
.unmodifiableCollection(this.distributedMap.entrySet(new SqlPredicate(this.distributedSql))));
|
||||
|
||||
case KEY:
|
||||
return getDistributedSQLResultSet(Collections
|
||||
.unmodifiableCollection(this.distributedMap.keySet(new SqlPredicate(this.distributedSQL))));
|
||||
.unmodifiableCollection(this.distributedMap.keySet(new SqlPredicate(this.distributedSql))));
|
||||
|
||||
case LOCAL_KEY:
|
||||
return getDistributedSQLResultSet(Collections
|
||||
.unmodifiableCollection(this.distributedMap.localKeySet(new SqlPredicate(this.distributedSQL))));
|
||||
.unmodifiableCollection(this.distributedMap.localKeySet(new SqlPredicate(this.distributedSql))));
|
||||
|
||||
default:
|
||||
return getDistributedSQLResultSet(this.distributedMap.values(new SqlPredicate(this.distributedSQL)));
|
||||
return getDistributedSQLResultSet(this.distributedMap.values(new SqlPredicate(this.distributedSql)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,9 +16,11 @@
|
||||
|
||||
package org.springframework.integration.hazelcast.inbound;
|
||||
|
||||
import java.util.EventObject;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.integration.hazelcast.common.HazelcastIntegrationDefinitionValidator;
|
||||
import org.springframework.integration.hazelcast.HazelcastHeaders;
|
||||
import org.springframework.integration.hazelcast.HazelcastIntegrationDefinitionValidator;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.hazelcast.core.DistributedObject;
|
||||
@@ -42,9 +44,9 @@ import com.hazelcast.core.ReplicatedMap;
|
||||
* to listen related cache events and sends events to related channel.
|
||||
*
|
||||
* @author Eren Avsarogullari
|
||||
* @author Artem Bilan
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public class HazelcastEventDrivenMessageProducer extends AbstractHazelcastMessageProducer {
|
||||
|
||||
public HazelcastEventDrivenMessageProducer(DistributedObject distributedObject) {
|
||||
@@ -58,32 +60,33 @@ public class HazelcastEventDrivenMessageProducer extends AbstractHazelcastMessag
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
protected void doStart() {
|
||||
if(this.distributedObject instanceof IMap) {
|
||||
if (this.distributedObject instanceof IMap) {
|
||||
setHazelcastRegisteredEventListenerId(((IMap<?, ?>) this.distributedObject)
|
||||
.addEntryListener(new HazelcastEntryListener(), true));
|
||||
}
|
||||
else if(this.distributedObject instanceof MultiMap) {
|
||||
else if (this.distributedObject instanceof MultiMap) {
|
||||
setHazelcastRegisteredEventListenerId(((MultiMap<?, ?>) this.distributedObject)
|
||||
.addEntryListener(new HazelcastEntryListener(), true));
|
||||
}
|
||||
else if(this.distributedObject instanceof ReplicatedMap) {
|
||||
else if (this.distributedObject instanceof ReplicatedMap) {
|
||||
setHazelcastRegisteredEventListenerId(((ReplicatedMap<?, ?>) this.distributedObject)
|
||||
.addEntryListener(new HazelcastEntryListener()));
|
||||
}
|
||||
else if(this.distributedObject instanceof IList) {
|
||||
else if (this.distributedObject instanceof IList) {
|
||||
setHazelcastRegisteredEventListenerId(((IList<?>) this.distributedObject)
|
||||
.addItemListener(new HazelcastItemListener(), true));
|
||||
}
|
||||
else if(this.distributedObject instanceof ISet) {
|
||||
else if (this.distributedObject instanceof ISet) {
|
||||
setHazelcastRegisteredEventListenerId(((ISet<?>) this.distributedObject)
|
||||
.addItemListener(new HazelcastItemListener(), true));
|
||||
}
|
||||
else if(this.distributedObject instanceof IQueue) {
|
||||
else if (this.distributedObject instanceof IQueue) {
|
||||
setHazelcastRegisteredEventListenerId(((IQueue<?>) this.distributedObject)
|
||||
.addItemListener(new HazelcastItemListener(), true));
|
||||
}
|
||||
else if(this.distributedObject instanceof ITopic) {
|
||||
else if (this.distributedObject instanceof ITopic) {
|
||||
setHazelcastRegisteredEventListenerId(((ITopic<?>) this.distributedObject)
|
||||
.addMessageListener(new HazelcastMessageListener()));
|
||||
}
|
||||
@@ -91,25 +94,25 @@ public class HazelcastEventDrivenMessageProducer extends AbstractHazelcastMessag
|
||||
|
||||
@Override
|
||||
protected void doStop() {
|
||||
if(this.distributedObject instanceof IMap) {
|
||||
if (this.distributedObject instanceof IMap) {
|
||||
((IMap<?, ?>) this.distributedObject).removeEntryListener(getHazelcastRegisteredEventListenerId());
|
||||
}
|
||||
else if(this.distributedObject instanceof MultiMap) {
|
||||
else if (this.distributedObject instanceof MultiMap) {
|
||||
((MultiMap<?, ?>) this.distributedObject).removeEntryListener(getHazelcastRegisteredEventListenerId());
|
||||
}
|
||||
else if(this.distributedObject instanceof ReplicatedMap) {
|
||||
else if (this.distributedObject instanceof ReplicatedMap) {
|
||||
((ReplicatedMap<?, ?>) this.distributedObject).removeEntryListener(getHazelcastRegisteredEventListenerId());
|
||||
}
|
||||
else if(this.distributedObject instanceof IList) {
|
||||
else if (this.distributedObject instanceof IList) {
|
||||
((IList<?>) this.distributedObject).removeItemListener(getHazelcastRegisteredEventListenerId());
|
||||
}
|
||||
else if(this.distributedObject instanceof ISet) {
|
||||
else if (this.distributedObject instanceof ISet) {
|
||||
((ISet<?>) this.distributedObject).removeItemListener(getHazelcastRegisteredEventListenerId());
|
||||
}
|
||||
else if(this.distributedObject instanceof IQueue) {
|
||||
else if (this.distributedObject instanceof IQueue) {
|
||||
((IQueue<?>) this.distributedObject).removeItemListener(getHazelcastRegisteredEventListenerId());
|
||||
}
|
||||
else if(this.distributedObject instanceof ITopic) {
|
||||
else if (this.distributedObject instanceof ITopic) {
|
||||
((ITopic<?>) this.distributedObject).removeMessageListener(getHazelcastRegisteredEventListenerId());
|
||||
}
|
||||
}
|
||||
@@ -119,7 +122,8 @@ public class HazelcastEventDrivenMessageProducer extends AbstractHazelcastMessag
|
||||
return "hazelcast:inbound-channel-adapter";
|
||||
}
|
||||
|
||||
private class HazelcastItemListener<E> extends AbstractHazelcastEventListener implements ItemListener<E> {
|
||||
private class HazelcastItemListener<E> extends AbstractHazelcastEventListener<ItemEvent<E>>
|
||||
implements ItemListener<E> {
|
||||
|
||||
@Override
|
||||
public void itemAdded(ItemEvent<E> item) {
|
||||
@@ -132,21 +136,29 @@ public class HazelcastEventDrivenMessageProducer extends AbstractHazelcastMessag
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processEvent(EventObject event) {
|
||||
Assert.notNull(event, "event must not be null");
|
||||
|
||||
if (getCacheEvents().contains(((ItemEvent<E>) event).getEventType().toString())) {
|
||||
sendMessage(event, ((ItemEvent<E>) event).getMember().getSocketAddress(), getCacheListeningPolicy());
|
||||
protected void processEvent(ItemEvent<E> event) {
|
||||
if (getCacheEvents().contains(event.getEventType().toString())) {
|
||||
sendMessage(event, event.getMember().getSocketAddress(), getCacheListeningPolicy());
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()){
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Received ItemEvent : " + event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected org.springframework.messaging.Message<?> toMessage(ItemEvent<E> event) {
|
||||
final Map<String, Object> headers = new HashMap<>();
|
||||
headers.put(HazelcastHeaders.EVENT_TYPE, event.getEventType().name());
|
||||
headers.put(HazelcastHeaders.MEMBER, event.getMember().getSocketAddress());
|
||||
|
||||
return getMessageBuilderFactory().withPayload(event.getItem()).copyHeaders(headers).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class HazelcastMessageListener<E> extends AbstractHazelcastEventListener implements MessageListener<E> {
|
||||
private class HazelcastMessageListener<E> extends AbstractHazelcastEventListener<Message<E>>
|
||||
implements MessageListener<E> {
|
||||
|
||||
@Override
|
||||
public void onMessage(Message<E> message) {
|
||||
@@ -154,15 +166,26 @@ public class HazelcastEventDrivenMessageProducer extends AbstractHazelcastMessag
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processEvent(EventObject event) {
|
||||
Assert.notNull(event, "event must not be null");
|
||||
sendMessage(event, ((Message<E>) event).getPublishingMember().getSocketAddress(), null);
|
||||
protected void processEvent(Message<E> event) {
|
||||
sendMessage(event, event.getPublishingMember().getSocketAddress(), null);
|
||||
|
||||
if (logger.isDebugEnabled()){
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Received Message : " + event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected org.springframework.messaging.Message<?> toMessage(Message<E> event) {
|
||||
Assert.notNull(event.getMessageObject(), "message must not be null");
|
||||
|
||||
final Map<String, Object> headers = new HashMap<>();
|
||||
headers.put(HazelcastHeaders.MEMBER, event.getPublishingMember().getSocketAddress());
|
||||
headers.put(HazelcastHeaders.CACHE_NAME, event.getSource());
|
||||
headers.put(HazelcastHeaders.PUBLISHING_TIME, event.getPublishTime());
|
||||
|
||||
return getMessageBuilderFactory().withPayload(event.getMessageObject()).copyHeaders(headers).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.net.SocketAddress;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import org.springframework.integration.hazelcast.common.HazelcastLocalInstanceRegistrar;
|
||||
import org.springframework.integration.hazelcast.HazelcastLocalInstanceRegistrar;
|
||||
|
||||
import com.hazelcast.core.Hazelcast;
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.integration.hazelcast.message;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Hazelcast Message Payload for Entry Events
|
||||
*
|
||||
* @author Eren Avsarogullari
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class EntryEventMessagePayload<K, V> {
|
||||
|
||||
public final K key;
|
||||
|
||||
public final V value;
|
||||
|
||||
public final V oldValue;
|
||||
|
||||
public EntryEventMessagePayload(final K key, final V value, final V oldValue) {
|
||||
Assert.notNull(key, "'key' must not be null");
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
this.oldValue = oldValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EntryEventMessagePayload [key=" + key + ", value=" + value + ", oldValue=" + oldValue + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
EntryEventMessagePayload<?, ?> that = (EntryEventMessagePayload<?, ?>) o;
|
||||
|
||||
return key.equals(that.key) && !(value != null ? !value.equals(that.value)
|
||||
: that.value != null) && !(oldValue != null
|
||||
? !oldValue.equals(that.oldValue) : that.oldValue != null);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = key.hashCode();
|
||||
result = 31 * result + (value != null ? value.hashCode() : 0);
|
||||
result = 31 * result + (oldValue != null ? oldValue.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Provides classes supporting Hazelcast message headers and payload.
|
||||
*/
|
||||
package org.springframework.integration.hazelcast.message;
|
||||
@@ -22,7 +22,7 @@ import java.util.Queue;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.integration.handler.AbstractMessageHandler;
|
||||
import org.springframework.integration.hazelcast.common.HazelcastIntegrationDefinitionValidator;
|
||||
import org.springframework.integration.hazelcast.HazelcastIntegrationDefinitionValidator;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -46,7 +46,7 @@ public class HazelcastCacheWritingMessageHandler extends AbstractMessageHandler
|
||||
private final DistributedObject distributedObject;
|
||||
|
||||
public HazelcastCacheWritingMessageHandler(DistributedObject distributedObject) {
|
||||
Assert.notNull(distributedObject, "cache must not be null");
|
||||
Assert.notNull(distributedObject, "'distributedObject' must not be null");
|
||||
this.distributedObject = distributedObject;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Provides common used types and classes.
|
||||
*/
|
||||
package org.springframework.integration.hazelcast;
|
||||
Reference in New Issue
Block a user