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

发布于 2024-12-05 07:51:42 字数 2471 浏览 0 评论 0原文

我想在另一台计算机上运行 OSGi 框架(在 main 方法中)。所以我想知道是否有任何方法可以从另一台计算机连接到 OSGi 控制台并管理捆绑包?

我想也许使用 java.net.Socket 会有所帮助,这就是我实现它的方式。我用了2个线程。一个用于处理用户输入流,另一个用于处理 OSGi 控制台响应。这是第一个线程(处理用户输入流):

    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();
    }

这是第二个线程(处理 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();
    }

   }
}

但我只收到 OSGi 控制台的第一个响应。像这样:


--READ LOOP

--

--READ LOOP

ss

--> ss


关于这个问题或任何其他远程连接到 OSGi 控制台的方法有什么想法吗?

I want to run OSGi framework on another computer (in a main method). So I wanted to know is there any way to connect to the OSGi console from the other computer and manage bundles?

I thought maybe using a java.net.Socket would help, and that's how I implemented that. I've used 2 threads. one for processing user input stream, and the other one that processes OSGi Console response. 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):

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. like this:


--READ LOOP

--

--READ LOOP

ss

--> ss


Any ideas about the problem or any other way to connect to OSGi console remotely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

究竟谁懂我的在乎 2024-12-12 07:51:42

您正在使用阻塞 io,因此您的内部 while 循环将永远完成,直到套接字关闭。您需要 2 个线程来通过阻塞 io 流来完成此操作。 1 个线程从 stdin 读取并写入套接字输出流,另一个线程从套接字输入流读取并写入 stdout。

另外,您可能想在将 userInput 发送到 osgi 控制台后编写一个换行符(Scanner.nextLine() 会吃掉换行符)。

最后,在使用套接字时,您通常不想使用 Print* 类,因为它们隐藏 IOException。

you are using blocking io, thus your inner while loop will never finish until the socket is closed. you need 2 threads to accomplish this with blocking io streams. 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.

also, you probably want to write a newline after sending the userInput to the osgi console (Scanner.nextLine() eats the newline).

lastly, you don't generally want to use the Print* classes when working with sockets as they hide IOExceptions.

合约呢 2024-12-12 07:51:42

您可能不想构建自己的东西,而是使用可用的远程 shell 之一,例如 http://felix.apache.org/site/apache-felix-remote-shell.html

Instead of building your own thing you might want to use one of the remote shells that are available, for example the Apache Felix one at http://felix.apache.org/site/apache-felix-remote-shell.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文