Search in sources :

Example 1 with CoherenceEventListener

use of com.oracle.coherence.spring.event.CoherenceEventListener in project coherence-spring by coherence-community.

the class MapListenerRegistrationBean method registerMapListeners.

/**
 * Listen for {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent.Type#CREATED Created}
 * {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent CacheLifecycleEvents}
 * and register relevant map listeners when caches are created.
 * @param event the {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent}
 */
@CoherenceEventListener
@SuppressWarnings({ "rawtypes", "unchecked" })
void registerMapListeners(@Created CacheLifecycleEvent event) {
    String cacheName = event.getCacheName();
    String eventScope = event.getScopeName();
    String eventSession = event.getSessionName();
    String eventService = event.getServiceName();
    Set<AnnotatedMapListener<?, ?>> setListeners = getMapListeners(removeScope(eventService), cacheName);
    Session session = Coherence.findSession(eventSession).orElseThrow(() -> new IllegalStateException("Cannot find a Session with name " + eventSession));
    NamedCache cache = session.getCache(cacheName);
    for (AnnotatedMapListener<?, ?> listener : setListeners) {
        if (listener.hasFilterAnnotation()) {
            // ensure that the listener's filter has been resolved as this
            // was not possible as discovery time.
            listener.setFilter(this.filterService.resolve(listener.getFilterAnnotations()));
        }
        if (listener.hasTransformerAnnotation()) {
            // ensure that the listener's transformer has been resolved as this
            // was not possible at discovery time.
            listener.resolveTransformer(this.mapEventTransformerService);
        }
        String sScope = listener.getScopeName();
        boolean fScopeOK = sScope == null || sScope.equals(eventScope);
        String sSession = listener.getSession();
        boolean fSessionOK = sSession == null || sSession.equals(eventSession);
        if (fScopeOK && fSessionOK) {
            Filter filter = listener.getFilter();
            if (filter != null && !(filter instanceof MapEventFilter)) {
                filter = new MapEventFilter(MapEventFilter.E_ALL, filter);
            }
            MapEventTransformer transformer = listener.getTransformer();
            if (transformer != null) {
                filter = new MapEventTransformerFilter(filter, transformer);
            }
            try {
                boolean fLite = listener.isLiteEvent();
                if (listener.isSynchronous()) {
                    cache.addMapListener(listener.synchronous(), filter, fLite);
                } else {
                    cache.addMapListener(listener, filter, fLite);
                }
            } catch (Exception ex) {
                throw Exceptions.ensureRuntimeException(ex);
            }
        }
    }
}
Also used : MapEventFilter(com.tangosol.util.filter.MapEventFilter) MapEventTransformer(com.tangosol.util.MapEventTransformer) MapEventTransformerFilter(com.tangosol.util.filter.MapEventTransformerFilter) NamedCache(com.tangosol.net.NamedCache) Filter(com.tangosol.util.Filter) MapEventFilter(com.tangosol.util.filter.MapEventFilter) MapEventTransformerFilter(com.tangosol.util.filter.MapEventTransformerFilter) Session(com.tangosol.net.Session) CoherenceEventListener(com.oracle.coherence.spring.event.CoherenceEventListener)

Example 2 with CoherenceEventListener

use of com.oracle.coherence.spring.event.CoherenceEventListener in project coherence-spring by coherence-community.

the class EventHandler method onOrderWithMultiplePropertyExtractors.

// end::on-order-with-property-extractor[]
// tag::on-order-with-multiple-property-extractors[]
@CoherenceEventListener
// <1>
@PropertyExtractor("customerId")
@PropertyExtractor("orderId")
public void onOrderWithMultiplePropertyExtractors(// <2>
@Inserted @MapName("orders") MapEvent<String, List<Object>> event) {
    // <3>
    List list = event.getNewValue();
    // <4>
    String customerId = (String) list.get(0);
    Long orderId = (Long) list.get(1);
// ...
}
Also used : List(java.util.List) PropertyExtractor(com.oracle.coherence.spring.annotation.PropertyExtractor) CoherenceEventListener(com.oracle.coherence.spring.event.CoherenceEventListener)

Aggregations

CoherenceEventListener (com.oracle.coherence.spring.event.CoherenceEventListener)2 PropertyExtractor (com.oracle.coherence.spring.annotation.PropertyExtractor)1 NamedCache (com.tangosol.net.NamedCache)1 Session (com.tangosol.net.Session)1 Filter (com.tangosol.util.Filter)1 MapEventTransformer (com.tangosol.util.MapEventTransformer)1 MapEventFilter (com.tangosol.util.filter.MapEventFilter)1 MapEventTransformerFilter (com.tangosol.util.filter.MapEventTransformerFilter)1 List (java.util.List)1