向Java HttpServer类的Java HttpHandler类传递数据或从Java HttpHandler类传递数据的正确方法 [英] The correct way to pass data to/from a java HttpHandler class for java HttpServer class

查看:765
本文介绍了向Java HttpServer类的Java HttpHandler类传递数据或从Java HttpHandler类传递数据的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于测试应用程序的java HttpHandler.在HttpHandler中,每个http请求都在单独的线程中处理,并传递给HttpExchange.但是我需要从主线程和类(用于设置HttpServer和HttpHandler的类)访问数据,以便HttpHandler可以为正在运行的当前测试发送回正确的响应.如何获得最佳数据传递方式或HttpHandler类可访问的数据?我无法向HttpHandler#handle添加另一个参数,因为该参数是由HttpHandler&定义的.由HttpServer使用,并且我无法在主类中访问任何静态方法.我还需要将消息从HttpHandler推回到主类进行记录.

I have a java HttpHandler that I am using to test an application. In the HttpHandler each http request is handled in a separate thread and is passed the HttpExchange. But I need to access data from the main thread and class (the one that setup the HttpServer and HttpHandler) so that HttpHandler can send back the correct response for the current test being run. How is the best way to get this data passed in or accessible by the HttpHandler class? I cannot add another parameter to the HttpHandler#handle since that is defined by the HttpHandler & used by the HttpServer, and I can not access none static methods in the main class. I will also need to push messages from the HttpHandler back to the main class to log.

谢谢

我的代码示例:

class RequestHandler implements HttpHandler {

    @Override
    public void handle(HttpExchange exchange)
     {
        // do a bunch of stuff with the request that come in.
    }
}


public class MainClass
{
    public static void main(String[] args)
    {
        HttpServer server;
        ExecutorService excutor;
        InetSocketAddress addr = new InetSocketAddress(ipAdd, ipPort);
        server = HttpServer.create(addr, 0);
        server.createContext("/", new RequestHandler());
        excutor = Executors.newCachedThreadPool();
        server.setExecutor(excutor);
        server.start();
       // do a bunch of stuff that uses the server
    }

推荐答案

从注释中可以看出,您是在自己构造处理程序.您可以将对象注入处理程序中的一种典型方法就是将它们作为参数传递给构造函数.

From the comments you say that you are constructing the handlers yourself. A typical way that you can inject objects into the handlers is just to pass them in as arguments to the constructor.

例如:

public class RequestHandler implements HttpHandler {

    private final Object someObject;

    public RequestHandler(Object someObject) {
        // there is an implied super() here
        this.someObject = someObject;
    }

    public void handle(HttpExchange exchange) throws IOException {
       ...
       // you can then use someObject here
       ...
    }
}

然后,您可以将对象传递到处理程序中,例如:

Then you can pass in the object into your handler like:

server.createContext("/", new RequestHandler(someObject));

就在之间处理程序之间传递信息而言,您应该能够使用HttpExchange.setAttribute(...)方法执行此操作.那是典型的方式.我建议使用以"_"开头的属性名称,以将其与HTTP属性区分开.

In terms of passing information around between handlers, you should be able to use the HttpExchange.setAttribute(...) method to do this. That is a typical way. I'd suggest using attribute names that start with "_" to differentiate them from HTTP attributes.

这篇关于向Java HttpServer类的Java HttpHandler类传递数据或从Java HttpHandler类传递数据的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆