JMX:从服务器读取属性 [英] JMX: Read attribute from Server

查看:142
本文介绍了JMX:从服务器读取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Adobe CQ(5.5)作为CMS。现在,我们的CQ环境包括一个作者服务器,用户可以在其中创建内容,以及2个发布服务器,用于将内容提供给互联网。

We're using Adobe CQ (5.5) as CMS. Now, our CQ environment consists of one author server, where users can create content, and 2 publish servers which serve the content to the internet.

现在有一个复制代理将内容从作者服务器推送到两个发布服务器。不幸的是,有些文章会阻止复制代理的队列,因此不再发布新内容。这不是什么大问题,因为它很容易修复。真正的问题是,在用户开始抱怨不再发布更改之前,我们没有注意到这种阻塞。

Now there's a replication agent which pushs content from the author server to both publish server. Unfortunately some articles block the queue of the replication agents, so no more new content is beeing published. This is not much of a problem, as it is easy to fix. The real problem is that we don't notice this blockage until users start to complain that no more changes are beeing published.

我在周围搜索并发现CQ提供了监视应用程序可以将其自身附加到的JMX API。然后我试图找到一些开源软件,它允许我配置警报,所以我们可以更快地做出反应,但我找不到东西。

I searched around and found out that CQ provides a JMX API where monitoring applications could attach itself to it. I then tried to find some open source software which would allow me to configure alerts, so we can react faster, but I couldn't find something.

这是我的时候决定我可以尝试编写自己的Java应用程序,它只读取属性并在属性为真时发送邮件。我想这比我想的要复杂。

This is when I decided that I could try to write my own Java Application which just reads the attribute and sends a mail if the attribute should be true. I guess that was more complicated than I tought.

首先,我不是Java开发人员,但由于CQ是基于Java的,我应该给它一试。我阅读了一些关于JMX和Java的文档,并且能够获得与CQ服务器的有效连接。但这几乎是我所能意识到的一切。

First off, I'm not a Java Developer, but since CQ is based on Java I tought I'd give it a try. I read some documentation about JMX and Java and was able to get a working connection to the CQ server. But this is almost everything I could realize.

我能够找到类 com.adobe.granite.replication 有一个类型代理,它存储每个复制代理的id(id将是复制代理的名称,例如 id =复制发布 - 1 )。每个复制代理都有不同的属性,但与我相关的属性是QueueBlocked。

I was able to find out that the class com.adobe.granite.replication has a type agent which stores an id for every replication agent (the id would be the name of the replication agent, for example id=replication-publish-1). Every replication-agent has different attributes, but the attribute relevant for me would be "QueueBlocked".

这是我到目前为止的代码(它基于< a href =http://docs.oracle.com/javase/1.5.0/docs/guide/jmx/examples/Basic/Client.java =nofollow>这个示例):

This is the code I've got so far (it's based on this example):

public static void main(String[] args) {
    try {
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://servername:9010/jmxrmi");
        JMXConnector jmxc = JMXConnectorFactory.connect(url, null);

        ClientListener listener = new ClientListener();

        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

        // This outputs the domains, one of them is com.adobee.granite.replication, the one which I need to use
        // This is why I'm sure that at least the connection works, I don't have any com.adobe.granite.replication class on my Eclipse installation, so the output has to come from the server
        String domains[] = mbsc.getDomains();
        for (int i = 0; i < domains.length; i++) {
            echo("\tDomain[" + i + "] = " + domains[i]);
        }

        ObjectName replication = new ObjectName("com.adobe.granite.replication:type=Agent,id=replication-publish-1");

        mbsc.getAttribute(replication, "QueueBlocked"); // This throws the error
} catch(Exception e) {

}

}

抛出的错误如下:

javax.management.InstanceNotFoundException: com.adobe.granite.replication:type=Agent,id=replication-publish-1

根据我的理解,我应该创建某种实例,但我真的不知道什么实例以及如何创建它。无论是文档还是代码片段,我都非常感谢能得到的任何帮助:)

From what I understand I should be creating some kind of instance, but I don't really have an idea what instance and how to create it. I'd really appreciate any help I can get no matter if it's a documentation or code snippet :)

推荐答案

解决了它:)

这是我正在使用的代码:

This is the code I'm using:

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import javax.management.Attribute;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class Client {

    public static void main(String[] args) {
        try {
            JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://servername:9010/jmxrmi");
            JMXConnector jmxc = JMXConnectorFactory.connect(url, null);

            MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

            ObjectName replication1 = new ObjectName("com.adobe.granite.replication:type=agent,id=\"replication-publish-1\"");
            ObjectName replication2 = new ObjectName("com.adobe.granite.replication:type=agent,id=\"replication-publish-2\"");

            String replication1Status = mbsc.getAttribute(replication1, "QueuePaused").toString();
            String replication2Status = mbsc.getAttribute(replication2, "QueuePaused").toString();



        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这篇关于JMX:从服务器读取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆