【问题标题】:Using Jolokia agent with an RMI-Server将 Jolokia 代理与 RMI-Server 一起使用
【发布时间】:2017-08-17 08:40:42
【问题描述】:

我在将 Jolokia 与 RMI 服务结合使用时遇到问题。一旦 RMI-Service 启动,Jolokia 就不能再通过 http 访问了。

我创建了一个示例类来重现问题:

package com.example.rmi;

import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

public class RMITest {

    private class RMIService extends UnicastRemoteObject {

        private static final long serialVersionUID = 1L;

        protected RMIService() throws RemoteException {
            super();
        }

        public void doStuff() {
            System.out.println("Processing...");

        }

    }

    public static void main(String[] args) throws RemoteException {
        RMITest rmiServer = new RMITest();
        rmiServer.init();
    }

    private void init() throws RemoteException {
        RMIService rmiService = new RMIService();

        try {
            System.out.println("Starting RMI-Service...");
            String hostname = InetAddress.getLocalHost().getHostName();
            System.setProperty("java.rmi.server.hostname", hostname);

            int port = 2005;
            LocateRegistry.createRegistry(port);
            Naming.rebind("rmi://" + hostname + ":" + port
                    + "/RMIService", rmiService);
            System.out.println("RMI-Service started!");

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

如果我将 main 方法更改为不启动 RMI-Service,Jolokia 可以再次通过 http URL http://127.0.0.1:8778/jolokia/ 访问:

public static void main(String[] args) throws RemoteException {
    RMITest rmiServer = new RMITest();
    //rmiServer.init();

    while(true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
   }

服务是一个可运行的 jar。这是我用来启动应用程序的命令:

java -javaagent:jolokia-jvm-1.3.7-agent.jar=port=8778,host=localhost -jar RMITest-0.0.1-SNAPSHOT.jar

我从官网下载了jolokia代理:Jolokia Agent Download

【问题讨论】:

    标签: java rmi jolokia


    【解决方案1】:

    我找到了一种解决方法,即在启动 RMI 服务后以编程方式启动代理。

    在下面的 Stackoverflow-Post 中描述了如何做到这一点:Starting a Java agent after program start

    所以我在运行 init-method 后启动了以下静态方法:

    public static void attachGivenAgentToThisVM(String pathToAgentJar) {
        try {
            String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
            String pid = nameOfRunningVM.substring(0, nameOfRunningVM.indexOf('@'));
            VirtualMachine vm = VirtualMachine.attach(pid);
            vm.loadAgent(pathToAgentJar, "");
            vm.detach();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    还需要对“tools.jar”的依赖:

    <dependency>
        <groupId>com.sun</groupId>
        <artifactId>tools</artifactId>
        <version>1.8</version>
        <scope>system</scope>
        <systemPath>${java.home}/../lib/tools.jar</systemPath>
    </dependency>
    

    我还是更喜欢直接从命令行启动代理。因此,非常感谢您提供更好的解决方案。

    【讨论】:

      猜你喜欢
      • 2015-02-22
      • 1970-01-01
      • 2013-01-10
      • 2018-11-02
      • 2014-03-06
      • 2016-12-09
      • 2013-06-11
      • 2012-06-08
      • 2023-03-22
      相关资源
      最近更新 更多