1
22
23 package com.liferay.portal.kernel.messaging.jmx;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.messaging.BaseDestinationEventListener;
28 import com.liferay.portal.kernel.messaging.Destination;
29 import com.liferay.portal.kernel.messaging.MessageBus;
30
31 import java.util.Collection;
32
33 import javax.management.InstanceAlreadyExistsException;
34 import javax.management.MBeanServer;
35 import javax.management.ObjectName;
36
37
43 public class JMXMessageListener extends BaseDestinationEventListener {
44
45 public void destinationAdded(Destination destination) {
46 try {
47 registerDestination(destination);
48 }
49 catch (Exception e) {
50 log.error(
51 "Unable to register destination " + destination.getName(), e);
52 }
53 }
54
55 public void destinationRemoved(Destination destination) {
56 try {
57 unregisterDestination(destination);
58 }
59 catch (Exception e) {
60 log.error(
61 "Unable to unregister destination " + destination.getName(), e);
62 }
63 }
64
65 public void destroy() throws Exception {
66 Collection<Destination> destinations = _messageBus.getDestinations();
67
68 for (Destination destination : destinations) {
69 try {
70 unregisterDestination(destination);
71 }
72 catch (Exception e) {
73 if (log.isWarnEnabled()) {
74 log.warn(
75 "Unable to unregister destination " +
76 destination.getName(),
77 e);
78 }
79 }
80 }
81
82 try {
83 _mBeanServer.unregisterMBean(
84 MessageBusManager.createObjectName());
85 }
86 catch (Exception e) {
87 if (log.isWarnEnabled()) {
88 log.warn("Unable to unregister message bus manager", e);
89 }
90 }
91 }
92
93 public void init() throws Exception {
94 if ((_mBeanServer == null) || (_messageBus == null)) {
95 throw new IllegalStateException(
96 "MBean server and message bus are not configured");
97 }
98
99 try {
100 _replaceMBeanRegistration(
101 new MessageBusManager(_messageBus),
102 MessageBusManager.createObjectName());
103 }
104 catch (Exception e) {
105 if (log.isWarnEnabled()) {
106 log.warn("Unable to register message bus manager", e);
107 }
108 }
109
110 Collection<Destination> destinations = _messageBus.getDestinations();
111
112 for (Destination destination : destinations) {
113 try {
114 registerDestination(destination);
115 }
116 catch (Exception e) {
117 if (log.isWarnEnabled()) {
118 log.warn(
119 "Unable to register destination " +
120 destination.getName(),
121 e);
122 }
123 }
124 }
125 }
126
127 public void setMBeanServer(MBeanServer mBeanServer) {
128 _mBeanServer = mBeanServer;
129 }
130
131 public void setMessageBus(MessageBus messageBus) {
132 _messageBus = messageBus;
133 }
134
135 protected void registerDestination(Destination destination)
136 throws Exception {
137
138 String destinationName = destination.getName();
139
140 _replaceMBeanRegistration(
141 new DestinationManager(destination),
142 DestinationManager.createObjectName(destinationName));
143
144 _replaceMBeanRegistration(
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 _mBeanServer.unregisterMBean(
155 DestinationManager.createObjectName(destinationName));
156
157 _mBeanServer.unregisterMBean(
158 DestinationStatisticsManager.createObjectName(destinationName));
159 }
160
161 private void _replaceMBeanRegistration(Object object, ObjectName objectName)
162 throws Exception {
163
164 try {
165 _mBeanServer.registerMBean(object, objectName);
166 }
167 catch (InstanceAlreadyExistsException iaee) {
168 _mBeanServer.unregisterMBean(objectName);
169
170 _mBeanServer.registerMBean(object, objectName);
171 }
172 }
173
174 private static Log log = LogFactoryUtil.getLog(JMXMessageListener.class);
175
176 private MBeanServer _mBeanServer;
177 private MessageBus _messageBus;
178
179 }