001
014
015 package com.liferay.portal.kernel.messaging.jmx;
016
017 import com.liferay.portal.kernel.jmx.MBeanRegistry;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.messaging.BaseDestinationEventListener;
021 import com.liferay.portal.kernel.messaging.Destination;
022 import com.liferay.portal.kernel.messaging.MessageBus;
023
024 import java.util.Collection;
025
026
030 public class JMXMessageListener extends BaseDestinationEventListener {
031
032 public void afterPropertiesSet() throws Exception {
033 if ((_mBeanRegistry == null) || (_messageBus == null)) {
034 throw new IllegalStateException(
035 "MBean server and message bus are not configured");
036 }
037
038 try {
039 _mBeanRegistry.replace(
040 _MESSAGE_BUS_MANAGER_OBJECT_NAME_CACHE_KEY,
041 new MessageBusManager(_messageBus),
042 MessageBusManager.createObjectName());
043 }
044 catch (Exception e) {
045 if (_log.isWarnEnabled()) {
046 _log.warn("Unable to register message bus manager", e);
047 }
048 }
049
050 Collection<Destination> destinations = _messageBus.getDestinations();
051
052 for (Destination destination : destinations) {
053 try {
054 registerDestination(destination);
055 }
056 catch (Exception e) {
057 if (_log.isWarnEnabled()) {
058 _log.warn(
059 "Unable to register destination " +
060 destination.getName(),
061 e);
062 }
063 }
064 }
065 }
066
067 @Override
068 public void destinationAdded(Destination destination) {
069 try {
070 registerDestination(destination);
071 }
072 catch (Exception e) {
073 _log.error(
074 "Unable to register destination " + destination.getName(), e);
075 }
076 }
077
078 @Override
079 public void destinationRemoved(Destination destination) {
080 try {
081 unregisterDestination(destination);
082 }
083 catch (Exception e) {
084 _log.error(
085 "Unable to unregister destination " + destination.getName(), e);
086 }
087 }
088
089 public void destroy() throws Exception {
090 Collection<Destination> destinations = _messageBus.getDestinations();
091
092 for (Destination destination : destinations) {
093 try {
094 unregisterDestination(destination);
095 }
096 catch (Exception e) {
097 if (_log.isWarnEnabled()) {
098 _log.warn(
099 "Unable to unregister destination " +
100 destination.getName(),
101 e);
102 }
103 }
104 }
105
106 try {
107 _mBeanRegistry.unregister(
108 _MESSAGE_BUS_MANAGER_OBJECT_NAME_CACHE_KEY,
109 MessageBusManager.createObjectName());
110 }
111 catch (Exception e) {
112 if (_log.isWarnEnabled()) {
113 _log.warn("Unable to unregister message bus manager", e);
114 }
115 }
116 }
117
118
121 @Deprecated
122 public void init() throws Exception {
123 afterPropertiesSet();
124 }
125
126 public void setMBeanRegistry(MBeanRegistry mBeanRegistry) {
127 _mBeanRegistry = mBeanRegistry;
128 }
129
130 public void setMessageBus(MessageBus messageBus) {
131 _messageBus = messageBus;
132 }
133
134 protected void registerDestination(Destination destination)
135 throws Exception {
136
137 String destinationName = destination.getName();
138
139 _mBeanRegistry.replace(
140 destinationName, new DestinationManager(destination),
141 DestinationManager.createObjectName(destinationName));
142
143 _mBeanRegistry.replace(
144 _getStatisticsObjectNameCacheKey(destinationName),
145 new DestinationStatisticsManager(destination),
146 DestinationStatisticsManager.createObjectName(destinationName));
147 }
148
149 protected void unregisterDestination(Destination destination)
150 throws Exception {
151
152 String destinationName = destination.getName();
153
154 _mBeanRegistry.unregister(
155 destinationName,
156 DestinationManager.createObjectName(destinationName));
157
158 _mBeanRegistry.unregister(
159 _getStatisticsObjectNameCacheKey(destinationName),
160 DestinationStatisticsManager.createObjectName(destinationName));
161 }
162
163 private String _getStatisticsObjectNameCacheKey(String destinationName) {
164 return destinationName + "statistics";
165 }
166
167 private static final String _MESSAGE_BUS_MANAGER_OBJECT_NAME_CACHE_KEY =
168 "messageBusManager";
169
170 private static final Log _log = LogFactoryUtil.getLog(
171 JMXMessageListener.class);
172
173 private MBeanRegistry _mBeanRegistry;
174 private MessageBus _messageBus;
175
176 }