/* * Copyright (c) 2010-2013 Evolveum * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.evolveum.midpoint.task.quartzimpl.execution; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import java.io.IOException; import java.io.InterruptedIOException; import java.net.SocketTimeoutException; import java.util.Map; import java.util.concurrent.*; /** * Source: Eamonn McManus * http://weblogs.java.net/blog/emcmanus/archive/2007/05/making_a_jmx_co.html * */ public class JmxClient { public static JMXConnector connectWithTimeout( final JMXServiceURL url, final Map<String,Object> env, long timeout, TimeUnit unit) throws IOException { final BlockingQueue<Object> mailbox = new ArrayBlockingQueue <Object>(1); ExecutorService executor = Executors.newSingleThreadExecutor(daemonThreadFactory); executor.submit(new Runnable() { public void run() { try { JMXConnector connector = JMXConnectorFactory.connect(url, env); if (!mailbox.offer(connector)) connector.close(); } catch (Throwable t) { mailbox.offer(t); } } }); Object result; try { result = mailbox.poll(timeout, unit); if (result == null) { if (!mailbox.offer("")) result = mailbox.take(); } } catch (InterruptedException e) { throw initCause(new InterruptedIOException(e.getMessage()), e); } finally { executor.shutdown(); } if (result == null) throw new SocketTimeoutException("Connect timed out: " + url); if (result instanceof JMXConnector) return (JMXConnector) result; try { throw (Throwable) result; } catch (IOException e) { throw e; } catch (RuntimeException e) { throw e; } catch (Error e) { throw e; } catch (Throwable e) { // In principle this can't happen but we wrap it anyway throw new IOException(e.toString(), e); } } private static <T extends Throwable> T initCause(T wrapper, Throwable wrapped) { wrapper.initCause(wrapped); return wrapper; } private static class DaemonThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { Thread t = Executors.defaultThreadFactory().newThread(r); t.setDaemon(true); return t; } } private static final ThreadFactory daemonThreadFactory = new DaemonThreadFactory(); }