Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibly more predictable and reliable way to communicate JMX URL JVM-to... #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
<version>0.0.3-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.7</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</project>

6 changes: 3 additions & 3 deletions src/main/java/com/hellblazer/process/JavaProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public interface JavaProcess extends ManagedProcess {
*/
File getJavaExecutable();

JMXConnector getLocalJmxConnector() throws ConnectException,
JMXConnector getLocalJmxConnector(String connectionName) throws ConnectException,
NoLocalJmxConnectionException;

MBeanServerConnection getLocalMBeanServerConnection() throws IOException,
MBeanServerConnection getLocalMBeanServerConnection(String connectionName) throws IOException,
NoLocalJmxConnectionException;

MBeanServerConnection getLocalMBeanServerConnection(Subject delegationSubject)
MBeanServerConnection getLocalMBeanServerConnection(String connectionName, Subject delegationSubject)
throws ConnectException,
NoLocalJmxConnectionException;

Expand Down
55 changes: 29 additions & 26 deletions src/main/java/com/hellblazer/process/impl/JavaProcessImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,27 @@
*/
package com.hellblazer.process.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.hellblazer.process.CannotStopProcessException;
import com.hellblazer.process.JavaProcess;
import com.hellblazer.process.ManagedProcess;
import com.hellblazer.process.NoLocalJmxConnectionException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.security.auth.Subject;

import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener;

import sun.management.ConnectorAddressLink;

import com.hellblazer.process.CannotStopProcessException;
import com.hellblazer.process.JavaProcess;
import com.hellblazer.process.ManagedProcess;
import com.hellblazer.process.NoLocalJmxConnectionException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.util.*;

/**
* @author Hal Hildebrand
Expand Down Expand Up @@ -257,7 +252,7 @@ public File getJavaExecutable() {
* @throws ConnectException
*/
@Override
public JMXConnector getLocalJmxConnector() throws ConnectException,
public JMXConnector getLocalJmxConnector(String connectorName) throws ConnectException,
NoLocalJmxConnectionException {
if (jmxc != null) {
return jmxc;
Expand All @@ -271,13 +266,21 @@ public JMXConnector getLocalJmxConnector() throws ConnectException,

String address;
try {
address = ConnectorAddressLink.importFrom(process.getPid());
VirtualMachine vm = VirtualMachine.attach("" + process.getPid());
Properties props = vm.getSystemProperties();
address = props.getProperty(connectorName);

if (address == null) {
throw new ConnectException("Unable to find address for remote JMX connection with name = " + connectorName);
}
} catch (IOException e) {
ConnectException cex = new ConnectException(
"Cannot obtain local JMX connector address of: "
+ this);
cex.initCause(e);
throw cex;
} catch (AttachNotSupportedException e) {
throw new RuntimeException(e);
}

if (address == null) {
Expand Down Expand Up @@ -332,10 +335,10 @@ public JMXConnector getLocalJmxConnector() throws ConnectException,
}

@Override
public MBeanServerConnection getLocalMBeanServerConnection()
public MBeanServerConnection getLocalMBeanServerConnection(String connectionName)
throws ConnectException,
NoLocalJmxConnectionException {
JMXConnector connector = getLocalJmxConnector();
JMXConnector connector = getLocalJmxConnector(connectionName);

try {
return connector.getMBeanServerConnection();
Expand All @@ -349,10 +352,10 @@ public MBeanServerConnection getLocalMBeanServerConnection()
}

@Override
public MBeanServerConnection getLocalMBeanServerConnection(Subject delegationSubject)
public MBeanServerConnection getLocalMBeanServerConnection(String connectionName, Subject delegationSubject)
throws ConnectException,
NoLocalJmxConnectionException {
JMXConnector connector = getLocalJmxConnector();
JMXConnector connector = getLocalJmxConnector(connectionName);

try {
return connector.getMBeanServerConnection(delegationSubject);
Expand Down
23 changes: 11 additions & 12 deletions src/test/java/com/hellblazer/process/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
package com.hellblazer.process;

import com.sun.jmx.remote.internal.RMIExporter;
import sun.rmi.server.UnicastServerRef;
import sun.rmi.server.UnicastServerRef2;

import javax.management.MBeanServer;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
Expand All @@ -28,17 +36,6 @@
import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServer;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import sun.management.ConnectorAddressLink;
import sun.rmi.server.UnicastServerRef;
import sun.rmi.server.UnicastServerRef2;

import com.sun.jmx.remote.internal.RMIExporter;

/**
* @author Hal Hildebrand
*
Expand All @@ -47,6 +44,7 @@
public class HelloWorld implements RMIExporter {

public static final String STARTUP_MSG = "HelloWorld startup successful";
public static final String JMX_CONNECTION_NAME = "com.salesforce.helloworld.jmx";

static void bindJmx() throws Exception {
JMXConnectorServer server;
Expand All @@ -63,7 +61,8 @@ static void bindJmx() throws Exception {
JMXServiceURL url = new JMXServiceURL("rmi", "127.0.0.1", 11645);
server = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
server.start();
ConnectorAddressLink.export(server.getAddress().toString());

System.setProperty(JMX_CONNECTION_NAME, server.getAddress().toString());
}

public static void main(String[] argv) throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/hellblazer/process/JavaProcessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void testLocalMBeanServerConnection() throws Exception {
@Override
public boolean isTrue() {
try {
connection = process.getLocalMBeanServerConnection();
connection = process.getLocalMBeanServerConnection(HelloWorld.JMX_CONNECTION_NAME);
return true;
} catch (ConnectException e) {
return false;
Expand Down