Calling JMX connection from thread in Java?

Nectos

The plan: Create admin thread and one for user, each of them could call upon connection in main. How can I call upon connection from my thread? Or should I be doing this connection in different place so my threads could see it? It should be something like this: mbeanProxy.startBattle(500);. That is how I call method from main class, but it does not work in threads tho. Server is JMX that uses MBeans.

Client.java:

    public class Client {
public static final String HOST = "localhost";
public static final String PORT = "9119";
public static Scanner sc;

    public static void main(String[] args) throws IOException, MalformedObjectNameException {
        sc = new Scanner(System.in);
        Map<String, String[]> env = new HashMap<>();
        System.out.println("Login: ");
        String login = sc.nextLine();
        System.out.println("Password: ");
        String password = sc.nextLine();
        String[] credentials = {login, password};
        env.put(JMXConnector.CREDENTIALS, credentials);
        JMXServiceURL url =
            new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + HOST + ":" + PORT + "/jmxrmi");
        JMXConnector jmxConnector = JMXConnectorFactory.connect(url, env);
        MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();
        ObjectName mbeanName = new ObjectName("serveris:type=Serveris");
        ServerisMBean mbeanProxy =
            (ServerisMBean) MBeanServerInvocationHandler.newProxyInstance(
                mbeanServerConnection, mbeanName, ServerisMBean.class, true);

        System.out.print( mbeanProxy.toString());
        Object dummyObject = new Object();
        Runnable task = new PlayerThread("test", dummyObject);
        Thread worker = new Thread(task);
        worker.start();
        mbeanProxy.startBattle(500);
        jmxConnector.close();
    }

}

ServerisMBean.java

package client;
public interface ServerisMBean {
    public void startBattle(int token);
    public void stopBattle();
    public int getToken();
}

And if someone notices any mistakes that I am making in my coding style please tell me, just a newbie and want to receive information on how I could become better at Java.

Nectos

All I had to do is to take "mbeanProxy" and send it to thread by this way:

In main: Runnable task = new PlayerThread("test", dummyObject, mbeanProxy);

In thread: You have to name it at top of code, private final ServerisMBean mbeanProxy;

Then in constructor at same thread: public PlayerThread(String vardas, Object dummyObject, ServerisMBean mbeanProxy1){ this.mbeanProxy = mbeanProxy1; }

And I can call it inside thread by:

public void run() {
        System.out.println("test:"+mbeanProxy.getToken());}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

calling main thread from Runnable thread in java

From Dev

Calling java method from different thread in ndk

From Java

Calling JMX MBean method from a shell script

From Dev

Cannot create JMX connection from JProfiler

From Java

Java : Thread calling Runnable

From Java

calling Thread.sleep() from synchronized context in Java

From Java

Calling into a saved java object via JNI from a different thread

From Java

Creating thread safe MyBatis sessions from a java.sql.Connection

From Java

Calling a method from the class that created a thread, in the thread

From Dev

Calling a method in a running thread, from outside the thread

From Dev

Calling a method in thread from another thread, python

From Java

Remote JMX connection

From Java

zabbix and jmx connection refused

From Dev

Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

From Java

Calling Rust from Java

From Java

Calling Java from Clojure

From Java

Calling SSJS from Java?

From Java

Calling Java from Python

From Java

Calling clojure from java

From Java

Calling Java from MATLAB?

From Java

Calling Java from MATLAB

From Dev

calling python from java

From Java

Calling @Transactional methods from another thread (Runnable)

From Dev

calling method from a new thread C

From Dev

Calling an FnMut callback from another thread

From Dev

Calling to a pointer from a c++ thread

From Dev

Signal is not calling slot from another thread

From Dev

Java thread not stopping even after calling interrupt

From Dev

If I call an asyncTask from a thread will the onPostExecute() execute on the thread or the calling activity

Related Related

HotTag

Archive