简体   繁体   中英

How to inject a worker class into a ServerSocket implementation?

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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