Search in sources :

Example 76 with JMXConnector

use of javax.management.remote.JMXConnector in project Activiti by Activiti.

the class JobExecutorJMXClientTest method testJobExecutorJMXClient.

@Test
public void testJobExecutorJMXClient() throws InterruptedException, IOException, MalformedObjectNameException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
    String hostName = Utils.getHostName();
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://" + hostName + ":10111/jndi/rmi://" + hostName + ":1099/jmxrmi/activiti");
    ProcessEngineConfiguration processEngineConfig = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    processEngineConfig.buildProcessEngine();
    // wait for jmx server to come up
    Thread.sleep(500);
    JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
    MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
    ObjectName jobExecutorBeanName = new ObjectName("org.activiti.jmx.Mbeans:type=JobExecutor");
    processEngineConfig.getJobExecutor().shutdown();
    // first check that job executor is not activated and correctly reported as being inactive
    assertFalse(processEngineConfig.isJobExecutorActivate());
    assertFalse((Boolean) mbsc.getAttribute(jobExecutorBeanName, "JobExecutorActivated"));
    // now activate it remotely
    mbsc.invoke(jobExecutorBeanName, "setJobExecutorActivate", new Boolean[] { true }, new String[] { Boolean.class.getName() });
    // check if it has the effect and correctly reported
    //    assertTrue(processEngineConfig.getJobExecutor().isActive());
    assertTrue((Boolean) mbsc.getAttribute(jobExecutorBeanName, "JobExecutorActivated"));
    //agani disable and check it    
    mbsc.invoke(jobExecutorBeanName, "setJobExecutorActivate", new Boolean[] { false }, new String[] { Boolean.class.getName() });
    // check if it has the effect and correctly reported
    assertFalse(processEngineConfig.isJobExecutorActivate());
    assertFalse((Boolean) mbsc.getAttribute(jobExecutorBeanName, "JobExecutorActivated"));
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) ProcessEngineConfiguration(org.activiti.engine.ProcessEngineConfiguration) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 77 with JMXConnector

use of javax.management.remote.JMXConnector in project midpoint by Evolveum.

the class RemoteNodesManager method getSchedulerBean.

private QuartzSchedulerMBean getSchedulerBean(NodeType node, Holder<JMXConnector> connectorHolder, OperationResult result) {
    String nodeName = node.getNodeIdentifier();
    String address = node.getHostname() + ":" + node.getJmxPort();
    try {
        JMXConnector connector = connectViaJmx(address);
        connectorHolder.setValue(connector);
        MBeanServerConnection serverConnection = connector.getMBeanServerConnection();
        QuartzSchedulerMBean bean = getMBeanProxy(nodeName, serverConnection);
        if (bean == null) {
            String message = "Cannot connect to the Quartz Scheduler bean at remote node " + nodeName + " at " + address + " because the JMX object for scheduler cannot be found on that node.";
            LOGGER.warn("{}", message);
            result.recordFatalError(message);
        }
        return bean;
    } catch (IOException | MalformedObjectNameException e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Cannot connect to the quartz scheduler bean at remote node {} at {}", e, nodeName, address);
        result.recordFatalError("Cannot connect to the quartz scheduler bean at remote node " + nodeName + " at " + address + ": " + e.getMessage(), e);
        return null;
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) JMXConnector(javax.management.remote.JMXConnector) IOException(java.io.IOException) MBeanServerConnection(javax.management.MBeanServerConnection) QuartzSchedulerMBean(org.quartz.core.jmx.QuartzSchedulerMBean)

Example 78 with JMXConnector

use of javax.management.remote.JMXConnector in project midpoint by Evolveum.

the class RemoteNodesManager method stopRemoteScheduler.

public void stopRemoteScheduler(String nodeIdentifier, OperationResult parentResult) {
    OperationResult result = parentResult.createSubresult(RemoteNodesManager.class.getName() + ".stopRemoteScheduler");
    result.addParam("nodeIdentifier", nodeIdentifier);
    NodeType node = getNode(nodeIdentifier, result);
    if (node == null) {
        return;
    }
    String nodeName = node.getNodeIdentifier();
    String address = node.getHostname() + ":" + node.getJmxPort();
    JMXConnector connector = null;
    try {
        MBeanServerConnection mbsc;
        try {
            connector = connectViaJmx(address);
            mbsc = connector.getMBeanServerConnection();
        } catch (IOException e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Cannot connect to the remote node {} at {}", e, nodeName, address);
            result.recordFatalError("Cannot connect to the remote node " + nodeName + " at " + address + ": " + e.getMessage(), e);
            return;
        }
        try {
            QuartzSchedulerMBean mbeanProxy = getMBeanProxy(nodeName, mbsc);
            if (mbeanProxy != null) {
                mbeanProxy.standby();
                result.recordSuccess();
            } else {
                result.recordWarning("Cannot stop the scheduler on node " + nodeName + " at " + address + " because the JMX object for scheduler cannot be found on that node.");
            }
            return;
        } catch (Exception e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Cannot put remote scheduler into standby mode; remote node {} at {}", e, nodeName, address);
            result.recordFatalError("Cannot put remote scheduler " + nodeName + " at " + address + " into standby mode: " + e.getMessage());
            return;
        }
    } finally {
        try {
            if (connector != null) {
                connector.close();
            }
        } catch (IOException e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Cannot close JMX connection to {}", e, address);
        }
    }
}
Also used : JMXConnector(javax.management.remote.JMXConnector) NodeType(com.evolveum.midpoint.xml.ns._public.common.common_3.NodeType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) IOException(java.io.IOException) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException) SystemException(com.evolveum.midpoint.util.exception.SystemException) QuartzSchedulerMBean(org.quartz.core.jmx.QuartzSchedulerMBean)

Example 79 with JMXConnector

use of javax.management.remote.JMXConnector in project midpoint by Evolveum.

the class RemoteNodesManager method redirectTaskToNode.

public void redirectTaskToNode(@NotNull Task task, @NotNull NodeType node, @NotNull OperationResult result) {
    LOGGER.trace("Trying to schedule task {} on {}", task, node.getNodeIdentifier());
    Holder<JMXConnector> connectorHolder = new Holder<>();
    try {
        QuartzSchedulerMBean mbeanProxy = getSchedulerBean(node, connectorHolder, result);
        if (mbeanProxy != null) {
            try {
                createStarterJobIfNeeded();
                mbeanProxy.triggerJob(STARTER_JOB_KEY.getName(), STARTER_JOB_KEY.getGroup(), Collections.singletonMap(JobStarter.TASK_OID, task.getOid()));
                LOGGER.debug("Successfully requested start of " + task + " at " + getClusterManager().dumpNodeInfo(node));
                result.recordSuccessIfUnknown();
            } catch (Exception e) {
                // necessary because of mbeanProxy
                String message = "Cannot schedule " + task + " at " + getClusterManager().dumpNodeInfo(node);
                LoggingUtils.logUnexpectedException(LOGGER, message, e);
                result.recordFatalError(message + ":" + e.getMessage(), e);
            }
        } else {
            LOGGER.warn("Couldn't obtain Quartz MBean so couldn't reschedule task {} on {}", task, node.getNodeIdentifier());
        }
    } finally {
        closeJmxConnection(connectorHolder, getClusterManager().dumpNodeInfo(node));
    }
}
Also used : JMXConnector(javax.management.remote.JMXConnector) Holder(com.evolveum.midpoint.util.Holder) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException) SystemException(com.evolveum.midpoint.util.exception.SystemException) QuartzSchedulerMBean(org.quartz.core.jmx.QuartzSchedulerMBean)

Example 80 with JMXConnector

use of javax.management.remote.JMXConnector in project jdk8u_jdk by JetBrains.

the class Client method run.

public static void run(String url) throws Exception {
    final int notifEmittedCnt = 10;
    final CountDownLatch counter = new CountDownLatch(notifEmittedCnt);
    final Set<Long> seqSet = Collections.synchronizedSet(new HashSet<Long>());
    final AtomicBoolean duplNotification = new AtomicBoolean();
    JMXServiceURL serverUrl = new JMXServiceURL(url);
    ObjectName name = new ObjectName("test", "foo", "bar");
    JMXConnector jmxConnector = JMXConnectorFactory.connect(serverUrl);
    System.out.println("client connected");
    jmxConnector.addConnectionNotificationListener(new NotificationListener() {

        @Override
        public void handleNotification(Notification notification, Object handback) {
            System.out.println("connection notification: " + notification);
            if (!seqSet.add(notification.getSequenceNumber())) {
                duplNotification.set(true);
            }
            if (notification.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {
                long lostNotifs = ((Long) ((JMXConnectionNotification) notification).getUserData()).longValue();
                for (int i = 0; i < lostNotifs; i++) {
                    counter.countDown();
                }
            }
        }
    }, null, null);
    MBeanServerConnection jmxServer = jmxConnector.getMBeanServerConnection();
    jmxServer.addNotificationListener(name, new NotificationListener() {

        @Override
        public void handleNotification(Notification notification, Object handback) {
            System.out.println("client got: " + notification);
            if (!seqSet.add(notification.getSequenceNumber())) {
                duplNotification.set(true);
            }
            counter.countDown();
        }
    }, null, null);
    System.out.println("client invoking foo (" + notifEmittedCnt + " times)");
    for (int i = 0; i < notifEmittedCnt; i++) {
        System.out.print(".");
        jmxServer.invoke(name, "foo", new Object[] {}, new String[] {});
    }
    System.out.println();
    try {
        System.out.println("waiting for " + notifEmittedCnt + " notifications to arrive");
        if (!counter.await(30, TimeUnit.SECONDS)) {
            throw new InterruptedException();
        }
        if (duplNotification.get()) {
            System.out.println("ERROR: received duplicated notifications");
            throw new Error("received duplicated notifications");
        }
        System.out.println("\nshutting down client");
    } catch (InterruptedException e) {
        System.out.println("ERROR: notification processing thread interrupted");
        throw new Error("notification thread interrupted unexpectedly");
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) CountDownLatch(java.util.concurrent.CountDownLatch) Notification(javax.management.Notification) JMXConnectionNotification(javax.management.remote.JMXConnectionNotification) ObjectName(javax.management.ObjectName) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JMXConnector(javax.management.remote.JMXConnector) JMXConnectionNotification(javax.management.remote.JMXConnectionNotification) MBeanServerConnection(javax.management.MBeanServerConnection) NotificationListener(javax.management.NotificationListener)

Aggregations

JMXConnector (javax.management.remote.JMXConnector)118 MBeanServerConnection (javax.management.MBeanServerConnection)85 JMXServiceURL (javax.management.remote.JMXServiceURL)78 ObjectName (javax.management.ObjectName)54 JMXConnectorServer (javax.management.remote.JMXConnectorServer)47 MBeanServer (javax.management.MBeanServer)37 IOException (java.io.IOException)35 HashMap (java.util.HashMap)27 Test (org.junit.Test)22 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)14 Attribute (javax.management.Attribute)13 MalformedURLException (java.net.MalformedURLException)12 RemoteException (java.rmi.RemoteException)11 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 MalformedObjectNameException (javax.management.MalformedObjectNameException)9 LocateRegistry (java.rmi.registry.LocateRegistry)8 Registry (java.rmi.registry.Registry)8 Properties (java.util.Properties)7