Event Driven Programming Java example tutorial style - part 2 JMX enabling & jconsole
This extends the basic tutorial on event driven programming in Java. This tutorial covers JMX enabling the EventHub class vian MBeans and monitoring through JMX compliant tool like jconsole.
Step 1: Define the interface with the methods that are intractable via the JMX compliant client like jconsole.
package com.writtentest14; public interface EventHubMBean { /** * Attributes */ long getEventCount(); /** * Operations */ void fireEventById(String eventId); }
Step 2: Define the implementation of the above interface. Note it extends StandardMBean from the Java management API.
package com.writtentest14; import javax.management.StandardMBean; public class StandardEventHubMBean extends StandardMBean implements EventHubMBean { private EventHub eventHub; public StandardEventHubMBean(EventHub eventHub) { super(EventHubMBean.class, false); this.eventHub = eventHub; } @Override public long getEventCount() { return this.eventHub.getEventCount(); } @Override public void fireEventById(String eventId) { Event event = new Event(eventId); this.eventHub.fire(event); } }
Step 3: Now, the revised EventHib with JMX enabled.
package com.writtentest14; import java.lang.management.ManagementFactory; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicLong; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; /** * register and unregister event listeners */ public class EventHub { private static final EventHub INSTANCE = createInstance(); private ConcurrentMap<String, List<EventListener<Event>>> registeredListeners = new ConcurrentHashMap<String, List<EventListener<Event>>>(); private EventDispatcher synchronousDispatcher; private AtomicLong eventCount = new AtomicLong(); public EventHub() { registerMBean(); } public static EventHub instance() { return INSTANCE; } public EventHub(EventDispatcher synchronousDispatcher) { this.synchronousDispatcher = synchronousDispatcher; registerMBean(); } public long getEventCount() { return this.eventCount.get(); } private long getNextEventNumber() { return this.eventCount.incrementAndGet(); } protected EventDispatcher getSynchronousDispatcher() { return this.synchronousDispatcher; } public void setSynchronousDispatcher(EventDispatcher dispatcher) { this.synchronousDispatcher = dispatcher; } public void fire(Event event) { dispatch(event, getSynchronousDispatcher()); } public synchronized void addListener(String eventId, EventListener<Event> listener) { List<EventListener<Event>> listeners = this.registeredListeners.get(eventId); if (listeners != null) { listeners.add(listener); } else { listeners = new CopyOnWriteArrayList<EventListener<Event>>(); listeners.add(listener); this.registeredListeners.put(eventId, listeners); } } public void removeListener(String eventId, EventListener<Event> listener) { List<EventListener<Event>> listeners = this.registeredListeners.get(eventId); if (listeners != null) { listeners.remove(listener); } } private void registerMBean() { MBeanServer server = getMBeanServer(); StandardEventHubMBean mbean = new StandardEventHubMBean(this); try { server.registerMBean(mbean, getMBeanObjectName()); } catch (Exception e) { e.printStackTrace(); } } protected void dispatch(Event event, EventDispatcher dispatcher) { getNextEventNumber(); List<EventListener<Event>> listeners = getListeners(event); if (!listeners.isEmpty()) { dispatcher.dispatch(event, listeners); } } private static EventHub createInstance() { EventHub instance = new EventHub(new SimpleSynchronousDispatcher()); return instance; } private List<EventListener<Event>> getListeners(Event event) { List<EventListener<Event>> listeners = this.registeredListeners.get(event.getId()); return (listeners != null) ? listeners : Collections.<EventListener<Event>>emptyList(); } private MBeanServer getMBeanServer() { ArrayList<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null); if (servers != null && !servers.isEmpty()) { return (MBeanServer) servers.get(0); } else { // If it all fails, get the platform default... return ManagementFactory.getPlatformMBeanServer(); } } private ObjectName getMBeanObjectName() { try { String name = "com.com.writtentest14:type=EventHub,instance=" + hashCode(); return new ObjectName(name); } catch (MalformedObjectNameException e) { throw new RuntimeException(e); } } }
Step 4: Run the EveneHubMain defined in part 1. Type jconsole from a DOS command prompt.
Step 5: You can now view the attributes and operations defined in EventHubMBean interface. It displays the number of events processed.
Step 6: You can also fire an event by entering an event name and clicking on fireEventById( ).
Labels: Architecture, design pattern, JMX
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home