简体   繁体   English

如何通过Java Socket发送命令并接收对OSGi控制台的响应?

[英]How to send commands and receive responses to OSGi console via a Java Socket?

I want to run OSGi framework on another computer (in a main method). 我想在另一台计算机上(以一种主要方法)运行OSGi框架。 So I wanted to know is there any way to connect to the OSGi console from the other computer and manage bundles? 所以我想知道有什么方法可以从另一台计算机连接到OSGi控制台并管理捆绑软件吗?

I thought maybe using a java.net.Socket would help, and that's how I implemented that. 我认为也许使用java.net.Socket会有所帮助,这就是我实现的方式。 I've used 2 threads. 我用了2个线程。 one for processing user input stream, and the other one that processes OSGi Console response. 一个用于处理用户输入流,另一个用于处理OSGi控制台响应。 This is the first thread (processes user input stream): 这是第一个线程(处理用户输入流):

    configMap.put("osgi.console", "6666");
    Framework fwk = ff.newFramework(configMap);
    try {
        fwk.start();
    } catch (BundleException e) {
        e.printStackTrace();
    }

//__________________________________________________________________//

    try {
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        Socket socket = new Socket(InetAddress.getByName("0.0.0.0"), 6666);
        printlnInfo("Socket has been created: " + socket.getInetAddress() + ":" + socket.getPort());
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        ConsoleOutputReciever fr = new ConsoleOutputReciever();
        new Thread(fr).start();
        while (true) {
            String userInput = "";
            while ((userInput = stdIn.readLine()) != null) {
                System.out.println("--> " + userInput);
                out.write(userInput + "\n");
                out.flush();
            }
            System.out.println("2");
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }

This is the second thread (processes OSGi Console response): 这是第二个线程(处理OSGi控制台响应):

public class ConsoleOutputReciever implements Runnable {

public Scanner in = null;

@Override
public void run() {
    printlnInfo("ConsoleOutputReciever Started");
    try {
        Socket socket = new Socket(InetAddress.getByName("0.0.0.0"), 6666);
        printlnInfo("Socket has been created: " + socket.getInetAddress() + ":" + socket.getPort());
        String osgiResponse = "";
        in = new Scanner(socket.getInputStream());
        try {
            while (true) {
                in = new Scanner(socket.getInputStream());
                while (in.hasNext()) {
                    System.out.println("-- READ LOOP");
                    osgiResponse = in.nextLine();
                    System.out.println("-- " + osgiResponse);
                }
            }
        } catch (IllegalBlockingModeException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

   }
}

but I only receive the first response of the OSGi console. 但是我只收到OSGi控制台的第一响应。 like this: 像这样:


--READ LOOP -阅读循环

-- -

--READ LOOP -阅读循环

ss ss

--> ss -> SS


Any ideas about the problem or any other way to connect to OSGi console remotely? 关于此问题的任何想法或远程连接到OSGi控制台的任何其他方式?

you are using blocking io, thus your inner while loop will never finish until the socket is closed. 您正在使用阻塞io,因此您的内部while循环将永远不会结束,直到关闭套接字为止。 you need 2 threads to accomplish this with blocking io streams. 您需要2个线程来阻止io流来完成此操作。 1 thread reads from stdin and writes to the socket output stream, the other thread reads from the socket input stream and writes to stdout. 1个线程从stdin读取并写入套接字输出流,另一个线程从socket输入流读取并写入stdout。

also, you probably want to write a newline after sending the userInput to the osgi console (Scanner.nextLine() eats the newline). 同样,您可能希望在将userInput发送到osgi控制台后写换行符(Scanner.nextLine()使用换行符)。

lastly, you don't generally want to use the Print* classes when working with sockets as they hide IOExceptions. 最后,在使用套接字时,通常不希望使用Print *类,因为它们会隐藏IOExceptions。

除了构建自己的东西外,您可能希望使用可用的远程外壳之一,例如,Apache Felix的http://felix.apache.org/site/apache-felix-remote-shell.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM