Crossfire JXClient, Trunk
ScanDirAgent.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of Oracle nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 package com.sun.jmx.examples.scandir;
42 
44 import java.io.IOException;
45 import java.lang.management.ManagementFactory;
46 import java.util.concurrent.BlockingQueue;
47 import java.util.concurrent.LinkedBlockingQueue;
48 import java.util.concurrent.TimeUnit;
49 import java.util.logging.Logger;
50 import javax.management.JMException;
51 import javax.management.Notification;
52 import javax.management.NotificationEmitter;
53 import javax.management.NotificationListener;
54 
76 public class ScanDirAgent {
77 
81  private static final Logger LOG =
82  Logger.getLogger(ScanDirAgent.class.getName());
83 
84  // Proxy to the ScanManagerMXBean - created by init();
85  //
86  private volatile ScanManagerMXBean proxy = null;
87 
88  // A queue to put received Notifications.
89  //
90  private final BlockingQueue<Notification> queue;
91 
92  // A listener that will put notifications into the queue.
93  //
94  private final NotificationListener listener;
95 
102  public ScanDirAgent() {
103  // Initialize the notification queue
104  queue = new LinkedBlockingQueue<Notification>();
105 
106  // Creates the listener.
107  listener = new NotificationListener() {
108  public void handleNotification(Notification notification,
109  Object handback) {
110  try {
111  // Just put the received notification in the queue.
112  // It will be consumed later on by 'waitForClose()'
113  //
114  LOG.finer("Queuing received notification "+notification);
115  queue.put(notification);
116  } catch (InterruptedException ex) {
117  // OK
118  }
119  }
120  };
121  }
122 
129  public void init() throws IOException, JMException {
130 
131  // Registers the ScanManagerMXBean singleton in the
132  // platform MBeanServer
133  //
135 
136  // Registers a NotificationListener with the ScanManagerMXBean in
137  // order to receive state changed notifications.
138  //
139  ((NotificationEmitter)proxy).addNotificationListener(listener,null,null);
140  }
141 
147  public void cleanup() throws IOException, JMException {
148  try {
149  ((NotificationEmitter)proxy).
150  removeNotificationListener(listener,null,null);
151  } finally {
152  ManagementFactory.getPlatformMBeanServer().
153  unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
154  }
155  }
156 
179  public void waitForClose() throws IOException, JMException {
180 
181  // Wait until state is closed
182  while(proxy.getState() != ScanState.CLOSED ) {
183  try {
184  // Wake up at least every 30 seconds - if we missed a
185  // notification - we will at least get a chance to
186  // call getState(). 30 seconds is obviously quite
187  // arbitrary - if this were a real daemon - id'be tempted
188  // to wait 30 minutes - knowing that any incoming
189  // notification will wake me up anyway.
190  // Note: we simply use the state change notifications to
191  // react more quickly to state changes: see javadoc above.
192  //
193  queue.poll(30,TimeUnit.SECONDS);
194  } catch (InterruptedException ex) {
195  // OK
196  }
197  }
198  }
199 
209  public static void main(String[] args)
210  throws IOException, JMException {
211  System.out.println("Initializing ScanManager...");
212  final ScanDirAgent agent = new ScanDirAgent();
213  agent.init();
214  try {
215  System.out.println("Waiting for ScanManager to close...");
216  agent.waitForClose();
217  } finally {
218  System.out.println("Cleaning up...");
219  agent.cleanup();
220  }
221  }
222 }
com.sun.jmx.examples.scandir.ScanDirAgent.listener
final NotificationListener listener
Definition: ScanDirAgent.java:94
com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState.CLOSED
CLOSED
Definition: ScanManagerMXBean.java:102
com.sun.jmx.examples.scandir.ScanDirAgent.cleanup
void cleanup()
Definition: ScanDirAgent.java:147
com.sun.jmx.examples.scandir
com.sun.jmx.examples.scandir.ScanManager.register
static ScanManagerMXBean register(MBeanServerConnection mbs)
Definition: ScanManager.java:275
com.sun.jmx.examples.scandir.ScanManager.SCAN_MANAGER_NAME
static final ObjectName SCAN_MANAGER_NAME
Definition: ScanManager.java:115
com.sun.jmx.examples.scandir.ScanDirAgent.main
static void main(String[] args)
Definition: ScanDirAgent.java:209
com.sun.jmx.examples.scandir.ScanManager
Definition: ScanManager.java:103
com.sun.jmx.examples.scandir.ScanDirAgent.LOG
static final Logger LOG
Definition: ScanDirAgent.java:81
com.sun.jmx.examples.scandir.ScanDirAgent.proxy
volatile ScanManagerMXBean proxy
Definition: ScanDirAgent.java:86
agent
versionCheck This agent library just makes some simple calls and checks the version of the interface being used to build the agent
Definition: README.txt:35
com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState
Definition: ScanManagerMXBean.java:77
com.sun.jmx.examples
com.sun.jmx.examples.scandir.ScanManagerMXBean
Definition: ScanManagerMXBean.java:66
com.sun.jmx.examples.scandir.ScanDirAgent.init
void init()
Definition: ScanDirAgent.java:129
com.sun
com.sun.jmx.examples.scandir.ScanManagerMXBean.getState
ScanState getState()
com.sun.jmx.examples.scandir.ScanDirAgent
Definition: ScanDirAgent.java:76
com
com.sun.jmx
com.sun.jmx.examples.scandir.ScanDirAgent.queue
final BlockingQueue< Notification > queue
Definition: ScanDirAgent.java:90
com.sun.jmx.examples.scandir.ScanDirAgent.waitForClose
void waitForClose()
Definition: ScanDirAgent.java:179
com.sun.jmx.examples.scandir.ScanDirAgent.ScanDirAgent
ScanDirAgent()
Definition: ScanDirAgent.java:102