1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.ksb.cache; |
18 | |
|
19 | |
import java.io.Serializable; |
20 | |
|
21 | |
import javax.xml.namespace.QName; |
22 | |
|
23 | |
import org.apache.commons.lang.StringUtils; |
24 | |
import org.apache.log4j.Logger; |
25 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
26 | |
import org.kuali.rice.ksb.api.KsbApiServiceLocator; |
27 | |
import org.kuali.rice.ksb.api.bus.ServiceBus; |
28 | |
import org.kuali.rice.ksb.api.bus.support.JavaServiceDefinition; |
29 | |
import org.kuali.rice.ksb.messaging.service.KSBJavaService; |
30 | |
|
31 | |
import com.opensymphony.oscache.base.Cache; |
32 | |
import com.opensymphony.oscache.base.Config; |
33 | |
import com.opensymphony.oscache.base.FinalizationException; |
34 | |
import com.opensymphony.oscache.base.InitializationException; |
35 | |
import com.opensymphony.oscache.plugins.clustersupport.AbstractBroadcastingListener; |
36 | |
import com.opensymphony.oscache.plugins.clustersupport.ClusterNotification; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class RiceDistributedCacheListener extends AbstractBroadcastingListener implements KSBJavaService { |
47 | |
|
48 | 0 | private static final Logger LOG = Logger.getLogger(RiceDistributedCacheListener.class); |
49 | |
|
50 | |
private String serviceName; |
51 | |
|
52 | |
protected ServiceBus serviceBus; |
53 | |
|
54 | |
@Override |
55 | |
public void initialize(Cache cache, Config config) throws InitializationException { |
56 | |
|
57 | 0 | LOG.info("Initializing cache listener"); |
58 | 0 | super.initialize(cache, config); |
59 | |
|
60 | |
|
61 | 0 | this.serviceName = config.getProperty(CacheProperties.SERVICE_NAME_KEY); |
62 | 0 | if (StringUtils.isBlank(this.serviceName)) { |
63 | 0 | throw new RiceRuntimeException("Cannot create DistributedCacheListener with empty serviceName"); |
64 | |
} |
65 | 0 | boolean forceRegistryRefresh = new Boolean((Boolean)config.getProperties().get(CacheProperties.FORCE_REGISTRY_REFRESH_KEY)); |
66 | 0 | serviceBus = (ServiceBus)config.getProperties().get(CacheProperties.SERVICE_BUS); |
67 | 0 | if (serviceBus == null) { |
68 | 0 | serviceBus = KsbApiServiceLocator.getServiceBus(); |
69 | |
} |
70 | 0 | LOG.info("Publishing Cache Service on bus under service name " + this.serviceName); |
71 | 0 | JavaServiceDefinition serviceDef = new JavaServiceDefinition(); |
72 | 0 | serviceDef.setPriority(3); |
73 | 0 | serviceDef.setRetryAttempts(1); |
74 | 0 | serviceDef.setService(this); |
75 | 0 | serviceDef.setLocalServiceName(this.serviceName); |
76 | 0 | serviceDef.setServiceNameSpaceURI(""); |
77 | 0 | serviceDef.setQueue(false); |
78 | |
try { |
79 | 0 | serviceDef.validate(); |
80 | 0 | } catch (Exception e) { |
81 | 0 | throw new RiceRuntimeException(e); |
82 | 0 | } |
83 | |
|
84 | 0 | serviceBus.publishService(serviceDef, forceRegistryRefresh); |
85 | 0 | } |
86 | |
|
87 | |
@Override |
88 | |
protected void sendNotification(ClusterNotification notification) { |
89 | 0 | if (LOG.isDebugEnabled()) { |
90 | 0 | LOG.debug("Sending cache notification " + notification); |
91 | |
} |
92 | |
try { |
93 | 0 | KSBJavaService oscacheNotificationService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(new QName(this.serviceName)); |
94 | 0 | oscacheNotificationService.invoke(notification); |
95 | 0 | } catch (Exception e) { |
96 | 0 | throw new RiceRuntimeException(e); |
97 | 0 | } |
98 | 0 | } |
99 | |
|
100 | |
@Override |
101 | |
public void finialize() throws FinalizationException { |
102 | |
|
103 | 0 | } |
104 | |
|
105 | |
@Override |
106 | |
public void invoke(Serializable payLoad) { |
107 | 0 | super.handleClusterNotification((ClusterNotification) payLoad); |
108 | 0 | } |
109 | |
|
110 | |
} |