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.Destination;
28 import com.liferay.portal.kernel.messaging.DestinationEventListener;
29 import com.liferay.portal.kernel.messaging.MessageBus;
30
31 import java.util.Collection;
32
33 import javax.management.MBeanServer;
34
35
42 public class JMXMessageListener implements DestinationEventListener {
43
44 public void destinationAdded(Destination destination) {
45 try {
46 registerDestination(destination);
47 }
48 catch (Exception e) {
49 log.error(
50 "Unable to register destination " + destination.getName(), e);
51 }
52 }
53
54 public void destinationRemoved(Destination destination) {
55 try {
56 unregisterDestination(destination);
57 }
58 catch (Exception e) {
59 log.error(
60 "Unable to unregister destination " + destination.getName(), e);
61 }
62 }
63
64 public void destroy() throws Exception {
65 _mBeanServer.unregisterMBean(
66 MessageBusManager.createObjectName());
67 }
68
69 public void init() throws Exception {
70 if ((_mBeanServer == null) || (_messageBus == null)) {
71 throw new IllegalStateException(
72 "MBean server and message bus are not configured");
73 }
74
75 _mBeanServer.registerMBean(
76 new MessageBusManager(_messageBus),
77 MessageBusManager.createObjectName());
78
79 Collection<Destination> destinations = _messageBus.getDestinations();
80
81 for (Destination destination : destinations) {
82 registerDestination(destination);
83 }
84 }
85
86 public void setMBeanServer(MBeanServer mBeanServer) {
87 _mBeanServer = mBeanServer;
88 }
89
90 public void setMessageBus(MessageBus messageBus) {
91 _messageBus = messageBus;
92 }
93
94 protected void registerDestination(Destination destination)
95 throws Exception {
96
97 String destinationName = destination.getName();
98
99 _mBeanServer.registerMBean(
100 new DestinationManager(destination),
101 DestinationManager.createObjectName(destinationName));
102
103 _mBeanServer.registerMBean(
104 new DestinationStatisticsManager(destination),
105 DestinationStatisticsManager.createObjectName(destinationName));
106 }
107
108 protected void unregisterDestination(Destination destination)
109 throws Exception {
110
111 String destinationName = destination.getName();
112
113 _mBeanServer.unregisterMBean(
114 DestinationManager.createObjectName(destinationName));
115
116 _mBeanServer.unregisterMBean(
117 DestinationStatisticsManager.createObjectName(destinationName));
118 }
119
120 private static Log log = LogFactoryUtil.getLog(JMXMessageListener.class);
121
122 private MBeanServer _mBeanServer;
123 private MessageBus _messageBus;
124
125 }