Skip to content

Commit

Permalink
STS-2952: JMX operations should be wrapped in a Job
Browse files Browse the repository at this point in the history
  • Loading branch information
leods committed Oct 4, 2012
1 parent 9753435 commit 0b071f8
Showing 1 changed file with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanServerConnection;
Expand All @@ -26,8 +28,10 @@
import javax.management.remote.JMXServiceURL;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.json.JSONException;
import org.springframework.context.support.LiveBeansViewMBean;
import org.springframework.ide.eclipse.beans.ui.livegraph.LiveGraphUiPlugin;
Expand All @@ -44,8 +48,10 @@ public class LiveBeansModelGenerator {
/**
* This method will not attempt to close the given {@link JMXConnector}. If
* the connection has failed, clients may capture the thrown
* {@link CoreException} and inform the user. This method will never return
* a <code>null</code> {@link LiveBeansModel}
* {@link CoreException} and inform the user. This method is not UI safe,
* and may block the UI with network operations. Clients will need to call
* this method from a non-blocking {@link Job}. This method will never
* return a <code>null</code> {@link LiveBeansModel}
*
* @param connector
* @param appName
Expand Down Expand Up @@ -87,7 +93,8 @@ public static LiveBeansModel connectToModel(JMXConnector connector, String appNa
* This method will attempt to create a {@link JMXConnector} from the given
* parameters and will close it when it is finished. If the connection has
* failed, clients may capture the thrown {@link CoreException} and inform
* the user. This method will never return a <code>null</code>
* the user. This method is UI safe, and will not block the UI with network
* operations. It will never return a <code>null</code>
* {@link LiveBeansModel}
*
* @param serviceUrl
Expand All @@ -98,24 +105,52 @@ public static LiveBeansModel connectToModel(JMXConnector connector, String appNa
* failed
* @throws CoreException
*/
public static LiveBeansModel connectToModel(String serviceUrl, String username, String password, String appName)
throws CoreException {
JMXConnector connector = null;
try {
connector = setupConnector(serviceUrl, username, password);
return connectToModel(connector, appName);
}
finally {
if (connector != null) {
public static LiveBeansModel connectToModel(final String serviceUrl, final String username, final String password,
final String appName) throws CoreException {
final CountDownLatch latch = new CountDownLatch(1);
final LiveBeansModel[] result = new LiveBeansModel[1];
final CoreException[] status = new CoreException[1];

Job jmxOperation = new Job("Executing Server Command") {
@Override
protected IStatus run(IProgressMonitor monitor) {
JMXConnector connector = null;
try {
connector.close();
connector = setupConnector(serviceUrl, username, password);
result[0] = connectToModel(connector, appName);
}
catch (CoreException e) {
status[0] = e;
}
catch (IOException e) {
StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
"An error occurred while closing connection to server.", e));
finally {
latch.countDown();
if (connector != null) {
try {
connector.close();
}
catch (IOException e) {
StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
"An error occurred while closing connection to server.", e));
}
}
}
return Status.OK_STATUS;
}
};
jmxOperation.schedule();

try {
if (latch.await(30, TimeUnit.SECONDS)) {
if (status[0] != null) {
throw status[0];
}
return result[0];
}
}
catch (InterruptedException e) {
// swallowed
}
return new LiveBeansModel();
}

private static LiveBeansModel generateModel(LiveBeansViewMBean mbean, String appName) throws CoreException {
Expand Down

0 comments on commit 0b071f8

Please sign in to comment.