Embedded Jetty: How to set the JMX port in source code - java

We have system tests and when they start an Embedded Jetty boots via the setup. The Embedded Jetty includes a JMX server, too. Then we have tests which must connect to the JMX Server via:
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:<JMX_PORT>/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
The problem is we cannot connect from the tests to the JMX server when we do not know the JMX port up front. Has anybody a clue how to specify the JMX port up front when the Embedded Jetty is built within the source code? The system property stuff is of no help here.

Related

Apache coyote vulnerability fixes are causing wrong port usage

I'm using an embedded tomcat server in one of my java project. the creation of server looks like
Tomcat _tomcat = new Tomcat();
i have configured the Tomcat server to use port numbers from 9001
everything was working fine.
Problem statement
To address the Apache coyote vulnerability which was addressed in a nessus scan i have modified the tomcat server creation to
Tomcat _tomcat = new Tomcat();
_tomcat.getConnector().setXpoweredBy(false);
_tomcat.getConnector().setProperty("server", "");
after doing this change tomcat server is binding to port number 8080, which i have not configured anywhere, this is causing issue when i try to run two instances at once. gives the below error. Tomcat server unable to bind error
Question
How to avoid tomcat server from using port 8080 with apache coyote vulnerability fixes?
Here when you do _tomcat.getConnector() a connector instance is called which is not yet initialized, and it uses default values and listens at port 8080 which is default port for a tomcat Connector instance.
instead of performing setXpoweredBy(false) and setProperty("server", "") on defualt instance of connector, move this part to where you are creating tomcat Connector instance. something like
Connector connector = new Connector();
connector.setPort(port);
connector.setXpoweredBy(false);
connector.setProperty("server", "");
and then set this connector to tomcat server
_tomcat.setConnector(connector);

How to customize the Jetty management server programmatically

I would like to implement a JettyServerCustomizer (or some other similar interface) to customize the management server (running on port 8081). The method customize(Server server) of the classes I implemented so far is called only for the main Jetty server (running on port 8080), when the application starts. How can I make it be called for the management server?
Enable JMX and/or JMX remote modules on standalone Jetty.
All of the various ThreadPools, along with many other components, will be available in JMX for you to access.
For embedded-jetty, do this ...
Server server = newServer();
MBeanContainer mbContainer = new MBeanContainer(
ManagementFactory.getPlatformMBeanServer());
server.addBean(mbContainer);
ConnectorServer jmx = new ConnectorServer(
new JMXServiceURL(
"rmi",
null,
1999,
"/jndi/rmi://localhost:1999/jmxrmi"),
"org.eclipse.jetty.jmx:name=rmiconnectorserver");
server.addBean(jmx);

JMX Client for Zookeeper

I am trying to workout a JMX Java client for Zookeeper instance for a custom monitoring web app. As provided in document, Zookeeper provides various statistics through JMX MBeans.
For the excercise, I am running Zookeeper intance locally in standalone mode on Windows 7 Enterprise using following arguments:-
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.port=10010
-Dzookeeper.jmx.log4j.disable=false
After running my zookeeper intance, I am able to connect to JMX beans using JConsole that correctly shows all the statistics :-
PROBLEM
While trying to connect using my own code I am getting java.net.ConnectException: Connection refused: connect error. Code that I am trying :-
public static void main(String[] args) throws Exception {
// service:jmx:rmi:///jndi/rmi://#{host}:#{port}/jmxrmi
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:10010/jmxrmi");
// This throws java.net.ConnectException !!!
JMXConnector jmxConnector = JMXConnectorFactory.connect(url);
MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();
ObjectName mbeanName = new ObjectName("org.apache.ZooKeeperService:name0=StandaloneServer_port2181");
ZooKeeperServerMXBean newProxyInstance = MBeanServerInvocationHandler.newProxyInstance(mbeanServerConnection,
mbeanName, ZooKeeperServerMXBean.class, true);
System.out.println("Created zoo mbean proxy");
System.out.println(newProxyInstance.getAvgRequestLatency());
}
Facing same problem while trying to connect using Java Visual VM.
What is the correct way to connect to Zookeeper MBean using Java code ?
UPDATE 1
There is 4 years old unresolved JIRA ticket that seems to be saying that there are two kind of ports that comes into play - jmx port & rmi port. The rmi port is generated randomly & I guess that is what needed while creating connection.
But then how JConsole is able to connect ?
UPDATE 2
This blog says that talking to remote JMX server over RMI protocol might be problem and suggests using JMXMP (JMX-Messaging Protocol) instead.
Now how do I exactly do I do that ?

How to get bound server address and port programmatically in Java EE?

At startup we need to get the server address and the http port of the running application. Until now we made it like this:
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName socketBindingMBean = new ObjectName("jboss.as:socket-binding-group=standard-sockets,socket-binding=http");
String host = (String) mBeanServer.getAttribute(socketBindingMBean, "boundAddress"),
Integer port = (Integer) mBeanServer.getAttribute(socketBindingMBean, "boundPort"));
Everything was fine but after migration from jBoss 7.1.1.Final to 7.1.3.Final we got the problem that the MBean isn't defined at server startup. That means everything is fine if I deploy the application on an already running jboss server, but if I start the server and the application is loaded up during server start MBeans are not there.
I don't know why but I have the feeling that jBoss makes sure, that out application is started/loaded before most of the MBeans. I had a small look and found out that following Mbeans are loaded after our application:
jboss.as:interface=..
jboss.as:socket-binding-group=..
jboss.as:subsystem=..
jboss.as:core-service=management.. (some)
So,
how can I force jBoss to load MBeans before my application?
is there another way/mbean where I can get my information?
I got the same issue in JBOSS Wildfly 8.1 . I solved the problem with the code below that worked for me to get server address and http port:
//http port
ManagementFactory.getPlatformMBeanServer().getAttribute(new ObjectName("jboss.as:socket-binding-group=standard-sockets,socket-binding=http"), "port");
//http adress
ManagementFactory.getPlatformMBeanServer().getAttribute(new ObjectName("jboss.as:interface=public"), "inet-address");

How to activate SSL(HTTPS) in Glassfish 3.0 embedded API?

We are implementing an application with a webservice as component and decided to use the Glassfish 3.0 embedded distri to provide the webservice. And it works.
We need a SSL(HTTPS) connection to the webservice, but we didn't find any documentation or hint how to activate it programmatically via the embedded API.
Thus we tried to configure the embedded Glassfish via domain.xml, what has a listener configured with SSL. And the port is reachable but only without SSL. The embedded Glassfish seem to ignore the configuration to activate SSL for the port.
Has anyone experience in configuring embedded Glassfish with SSL?
Ok,
sorry that it took so much time for my answer.
The programmatical embedded API seems not to porvide a way to do this task.
Except to run an asadmin command:
logger.debug("Configure port for SSL");
String command = "create-http-listener";
ParameterMap params = new ParameterMap();
params.add("listeneraddress", "0.0.0.0");
params.add("listenerport", "443");
params.add("defaultvs", "server");
params.add("securityenabled", "true");
params.add("enabled", "true");
params.add("DEFAULT", "http-listener2");
CommandRunner runner = server.getHabitat().getComponent(CommandRunner.class);
ActionReport report = server.getHabitat().getComponent(ActionReport.class);
runner.getCommandInvocation(command, report).parameters(params).execute();
Running this code is simmlar to execute:
asadmin create-http-listener --listeneraddress 0.0.0.0 --listenerport 443 --defaultvs server securityenabled=true --enabled=true http-listener2
But this solution creates a new port with SSL. Reconfigure the already started port would be a nice option.

Categories