如何将工作类注入到 ServerSocket 实现中?
我希望使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太清楚你已经走了多远,所以请原谅那些明显的陈述。
您可以通过多种方式到达这里。这在很大程度上取决于您的服务器框架想要为调用者做多少工作。例如,它可以只接受套接字,然后使用
Socket
或关联的输入和输出流调用handle
。或者您的框架可以采用一个对象和一组接口来进行代理。客户端可以发送一个字符串和一个对象数组,您可以调用注入代理上的方法。您似乎在暗示某些客户端可以作为代理接口处理,但其他客户端需要对输入/输出流进行较低级别的访问。因此,您可以拥有最简单的
ClientHandler
接口,客户端必须实现如下所示的接口:然后您的框架可以提供各种实现/包装器。其中之一可以是代理处理程序,它将客户端调用作为
ObjectInputStream
和输出流进行处理。客户端可以编写方法名称和参数的对象数组:框架可以提供处理逐行协议等的其他处理程序。
和包装器:
所以基本处理程序将是 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 theSocket
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: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:The framework could provide other handlers that dealt with line-by-line protocols and the like.
And the wrapper:
So the basic handler would be the
ClientHandler
and the more features could be provided by theProxyHandler
,LineByLineHandler
, etc..Hope this helps. Sorry if this is all obvious. Comment and I'll increase the level of specificity.