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 public void init() throws Exception {
122 afterPropertiesSet();
123 }
124
125 public void setMBeanRegistry(MBeanRegistry mBeanRegistry) {
126 _mBeanRegistry = mBeanRegistry;
127 }
128
129 public void setMessageBus(MessageBus messageBus) {
130 _messageBus = messageBus;
131 }
132
133 protected void registerDestination(Destination destination)
134 throws Exception {
135
136 String destinationName = destination.getName();
137
138 _mBeanRegistry.replace(
139 destinationName, new DestinationManager(destination),
140 DestinationManager.createObjectName(destinationName));
141
142 _mBeanRegistry.replace(
143 _getStatisticsObjectNameCacheKey(destinationName),
144 new DestinationStatisticsManager(destination),
145 DestinationStatisticsManager.createObjectName(destinationName));
146 }
147
148 protected void unregisterDestination(Destination destination)
149 throws Exception {
150
151 String destinationName = destination.getName();
152
153 _mBeanRegistry.unregister(
154 destinationName,
155 DestinationManager.createObjectName(destinationName));
156
157 _mBeanRegistry.unregister(
158 _getStatisticsObjectNameCacheKey(destinationName),
159 DestinationStatisticsManager.createObjectName(destinationName));
160 }
161
162 private String _getStatisticsObjectNameCacheKey(String destinationName) {
163 return destinationName + "statistics";
164 }
165
166 private static final String _MESSAGE_BUS_MANAGER_OBJECT_NAME_CACHE_KEY =
167 "messageBusManager";
168
169 private static Log _log = LogFactoryUtil.getLog(JMXMessageListener.class);
170
171 private MBeanRegistry _mBeanRegistry;
172 private MessageBus _messageBus;
173
174 }