如何将工作类注入到 ServerSocket 实现中?

发布于 2024-12-23 09:58:21 字数 526 浏览 3 评论 0原文

我希望使用 ServerSocket 创建一个通用的 TCP/IP 服务器,可供我的团队正在进行的多个项目使用。我面临的障碍是如何最好地注入可运行工作线程类,因为不同的项目将具有不同的与客户端交互的功能。

这是我现在得到的:

Socket clientSocket = serverSocket.accept();
Runnable worker = WorkerFactory.getWorker(interfaceId, clientSocket, id);
Thread t = new Thread(worker, "Client #" + id);
t.start();

interfaceId 传递到服务器适用于项目的子集,但是当考虑所有可能的用途时,这不是一个可行的解决方案。

我认为使用泛型的工厂,或者可能以某种方式使用 Spring 框架,可能是可行的方法,但我对如何去做有点模糊。我进行了搜索,但找不到任何有关为 ServerSocket 实现注入工作人员的信息。任何建议将不胜感激...

I'm looking to create a generic TCP/IP server using ServerSocket that can be used by multiple projects my team is working on. The hurdle I'm facing is how best to inject a Runnable worker class since various projects will have different functionality for interacting with the client.

Here's what I've got now:

Socket clientSocket = serverSocket.accept();
Runnable worker = WorkerFactory.getWorker(interfaceId, clientSocket, id);
Thread t = new Thread(worker, "Client #" + id);
t.start();

Passing the interfaceId into the server works for a subset of the projects, but when all the possible uses are considered it's not a viable solution.

I'm thinking a factory using generics, or possibly somehow using the Spring framework, may be the way to go but I'm a little vague on how to go about that. I've searched and I've been unable to find anything about injecting workers for a ServerSocket implementation. Any suggestions would be appreciated...

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

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

发布评论

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

评论(1

永不分离 2024-12-30 09:58:21

我不太清楚你已经走了多远,所以请原谅那些明显的陈述。

您可以通过多种方式到达这里。这在很大程度上取决于您的服务器框架想要为调用者做多少工作。例如,它可以只接受套接字,然后使用 Socket 或关联的输入和输出流调用 handle。或者您的框架可以采用一个对象和一组接口来进行代理。客户端可以发送一个字符串和一个对象数组,您可以调用注入代理上的方法。

您似乎在暗示某些客户端可以作为代理接口处理,但其他客户端需要对输入/输出流进行较低级别的访问。因此,您可以拥有最简单的 ClientHandler 接口,客户端必须实现如下所示的接口:

public interface ClientHandler {
    public void handleClient(InputStream, OutputStream) throws Exception;
}

然后您的框架可以提供各种实现/包装器。其中之一可以是代理处理程序,它将客户端调用作为 ObjectInputStream 和输出流进行处理。客户端可以编写方法名称和参数的对象数组:

public class ProxyHandler implements ClientHandler {
    private Object handler;
    public ProxyHandler(Object handler) {
       this.handler = handler;
    }
    public void handleClient(InputStream input, OutputStream output) throws Exception {
       // creates the object i/o streams
       // reads method string, object array, looks up methods in the handler obj by name
       // loop until done
    }
}

框架可以提供处理逐行协议等的其他处理程序。

public interface LineByLineHandler {
    public String handleLine(String line);
}

和包装器:

public class LineByLineHandlerWrapper implements ClientHandler {
    private LineByLineHandler handler;
    public ProxyHandler(LineByLineHandler handler) {
       this.handler = handler;
    }
    public void handleClient(InputStream input, OutputStream output) throws Exception {
       // creates the buffered i/o reader/writes
       // reads a string string, calls the handler, writes the response
       // loop until done
    }
}

所以基本处理程序将是 ClientHandler,更多功能可以由 ProxyHandler、LineByLineHandler 等提供。

希望这有帮助。抱歉,如果这一切都是显而易见的。发表评论,我会提高具体程度。

I can't quite tell how far along you are here so excuse the obvious statements.

There are a number of ways that you can go here. It depends a lot on how much work your server framework wants to do for the callers. For example, it could just accept the sockets and then call handle with the Socket or the associated input and output streams. Or your framework could take an object and an array of interfaces to proxy. The client could send a string and an array of objects and you could invoke the methods on injected proxies.

It seems like you are implying that some clients can be handled as a proxy interface but others need lower level access to the input/output streams. So you could have your simplest ClientHandler interface that the clients have to implement something like:

public interface ClientHandler {
    public void handleClient(InputStream, OutputStream) throws Exception;
}

Your framework could then provide various implementations/wrappers. One could be a proxy handler which would handle clients calls as an ObjectInputStream and output stream. The client could write a method-name and the object array for the arguments:

public class ProxyHandler implements ClientHandler {
    private Object handler;
    public ProxyHandler(Object handler) {
       this.handler = handler;
    }
    public void handleClient(InputStream input, OutputStream output) throws Exception {
       // creates the object i/o streams
       // reads method string, object array, looks up methods in the handler obj by name
       // loop until done
    }
}

The framework could provide other handlers that dealt with line-by-line protocols and the like.

public interface LineByLineHandler {
    public String handleLine(String line);
}

And the wrapper:

public class LineByLineHandlerWrapper implements ClientHandler {
    private LineByLineHandler handler;
    public ProxyHandler(LineByLineHandler handler) {
       this.handler = handler;
    }
    public void handleClient(InputStream input, OutputStream output) throws Exception {
       // creates the buffered i/o reader/writes
       // reads a string string, calls the handler, writes the response
       // loop until done
    }
}

So the basic handler would be the ClientHandler and the more features could be provided by the ProxyHandler, LineByLineHandler, etc..

Hope this helps. Sorry if this is all obvious. Comment and I'll increase the level of specificity.

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