JAVA RESTful WebService實(shí)戰(zhàn)筆記(四)

前言

在JAVA RESTful WebService實(shí)戰(zhàn)筆記(三)中已經(jīng)完成了對JAX-RS2定義的4中過濾器的講述學(xué)習(xí),以下就來看看如何綜合運(yùn)用過濾器,完成一個(gè)記錄REST請求的訪問日志

訪問日志(最新版沒有AirLogFilter,應(yīng)該是LoggingFilter)

  • LoggingFilter實(shí)現(xiàn)了上述的4個(gè)過濾器,記錄服務(wù)器端和客戶端的請求和響應(yīng)運(yùn)行時(shí)候的信息,LoggingFilter類的定義如下所示:
public final class LoggingFilter implements ContainerRequestFilter, ClientRequestFilter, ContainerResponseFilter,
                                            ClientResponseFilter, WriterInterceptor {
  • LoggingFilter為每一種過濾器接口定義的filter()方法提供了實(shí)現(xiàn),并且也實(shí)現(xiàn)了寫入的攔截器。在客戶端請求過濾中,輸出請求資源地址信息和請求投信息;在容器請求過濾中,輸出請求方法,請求資源地址信息和請求頭信息;在容器響應(yīng)過濾中,輸出HTTP狀態(tài)碼和請求頭信息;在客戶端響應(yīng)過濾中,輸出HTTP狀態(tài)碼和請求頭信息,4個(gè)階段的filter()示例代碼如下:
 @Override
    public void filter(final ClientRequestContext context) throws IOException {
        final long id = _id.incrementAndGet();
        context.setProperty(LOGGING_ID_PROPERTY, id);
    
        final StringBuilder b = new StringBuilder();
        //獲取請求方法和地址
        printRequestLine(b, "Sending client request", id, context.getMethod(), context.getUri());
        //獲取請求頭信息
        printPrefixedHeaders(b, id, REQUEST_PREFIX, context.getStringHeaders());

        if (printEntity && context.hasEntity()) {
            final OutputStream stream = new LoggingStream(b, context.getEntityStream());
            context.setEntityStream(stream);
            context.setProperty(ENTITY_LOGGER_PROPERTY, stream);
            // not calling log(b) here - it will be called by the interceptor
        } else {
            log(b);
        }
    }

 @Override
    public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext)
            throws IOException {
        final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY);
        final long id = requestId != null ? (Long) requestId : _id.incrementAndGet();

        final StringBuilder b = new StringBuilder();
        //獲取容器響應(yīng)狀態(tài)
        printResponseLine(b, "Client response received", id, responseContext.getStatus());
        //獲取容器響應(yīng)頭信息
        printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getHeaders());
  • 單元測試類
public class TIResourceJtfTest extends JerseyTest {
    @Override
    protected Application configure() {
        ResourceConfig config = new ResourceConfig(BookResource.class);
        return config.register(com.example.filter.log.AirLogFilter.class);
    }
    @Override
    protected void configureClient(ClientConfig config) {
        config.register(new AirLogFilter());
    }

上述代碼中,為了訪問日志生效,需要測試類TIResourceJtfTest在Jersey測試框架的服務(wù)器端和客戶端,分別注冊服務(wù)日志類AirLogFilter,單元測試結(jié)果如下

2017-09-13 10:01:58,135 DEBUG [com.example.resource.TIResourceJtfTest] main - >>Test Post
2017-09-13 10:01:58,232 INFO  [com.example.filter.log.AirLogFilter] main - 1 * AirLog - Request received on thread main
1 / POST http://localhost:9998/books/
1 / Accept: application/json
1 / Content-Type: application/json

2017-09-13 10:01:59,038 DEBUG [com.example.resource.interceptor.AirReaderWriterInterceptor] grizzly-http-server-0 - null:Java Restful Web Service實(shí)戰(zhàn)-602865027284019:null
2017-09-13 10:01:59,043 DEBUG [com.example.resource.interceptor.AirReaderWriterInterceptor] grizzly-http-server-0 - 602865932131718:Java Restful Web Service實(shí)戰(zhàn)-602865027284019:null
2017-09-13 10:01:59,084 INFO  [com.example.filter.log.AirLogFilter] main - 2 * AirLog - Response received on thread main
2 \ 200
2 \ Content-Length: 86
2 \ Date: Wed, 13 Sep 2017 02:01:59 GMT
2 \ Content-Type: application/json

2017-09-13 10:01:59,096 DEBUG [com.example.resource.TIResourceJtfTest] main - <<Test Post
九月 13, 2017 10:01:59 上午 org.glassfish.grizzly.http.server.NetworkListener shutdownNow
信息: Stopped listener bound to [localhost:9998]

REST攔截器

攔截器和過濾器的相同點(diǎn)就是都是一種在請求--響應(yīng)模型中,用作切面處理的Provider。兩者的不同除了功能性上的差異(一個(gè)用于過濾消息,一個(gè)用于攔截處理)之外,形式上也不同,攔截器通常都是"讀寫"成對,而且沒有服務(wù)器端和客戶端的區(qū)分。Jersey提供的攔截器如下:


image.png
1、ReaderInterceptor
  • 讀攔截器ReaderInterceptor定義的攔截方法是aroundReadFrom(),該方法包含一個(gè)輸入?yún)?shù),即讀攔截器的上下文接口ReaderInterceptorContext,從中可以獲取到投信息,輸入流以及父接口InterceptorContext提供的媒體類型等上下文信息。接口方法示例如下:
 /**
     * Interceptor method wrapping calls to {@link MessageBodyReader#readFrom} method.
     *
     * The parameters of the wrapped method called are available from {@code context}.
     * Implementations of this method SHOULD explicitly call {@link ReaderInterceptorContext#proceed}
     * to invoke the next interceptor in the chain, and ultimately the wrapped
     * {@link MessageBodyReader#readFrom} method.
     *
     * @param context invocation context.
     * @return result of next interceptor invoked or the wrapped method if last interceptor in chain.
     * @throws java.io.IOException if an IO error arises or is thrown by the wrapped
     *                             {@code MessageBodyReader.readFrom} method.
     * @throws javax.ws.rs.WebApplicationException
     *                             thrown by the wrapped {@code MessageBodyReader.readFrom} method.
     */
    public Object aroundReadFrom(ReaderInterceptorContext context)
            throws java.io.IOException, javax.ws.rs.WebApplicationException;
2、WriterInterceptor

寫攔截器WriterInterceptor定義的攔截方法是aroundWriteTo(),該方法包含一個(gè)輸入?yún)?shù),寫攔截器上下文接口WriterInterceptorContext,從中可以獲取投信息,輸出流以及父接口InterceptorContext提供的媒體類型等上下文信息,接口方法示例如下:

 /**
     * Interceptor method wrapping calls to {@link MessageBodyWriter#writeTo} method.
     * The parameters of the wrapped method called are available from {@code context}.
     * Implementations of this method SHOULD explicitly call
     * {@link WriterInterceptorContext#proceed} to invoke the next interceptor in the chain,
     * and ultimately the wrapped {@code MessageBodyWriter.writeTo} method.
     *
     * @param context invocation context.
     * @throws java.io.IOException if an IO error arises or is thrown by the wrapped
     *                             {@code MessageBodyWriter.writeTo} method.
     * @throws javax.ws.rs.WebApplicationException
     *                             thrown by the wrapped {@code MessageBodyWriter.writeTo} method.
     */
    void aroundWriteTo(WriterInterceptorContext context)
            throws java.io.IOException, javax.ws.rs.WebApplicationException;

REST服務(wù)與異步

服務(wù)端實(shí)現(xiàn)

可以利用JAX-RS2提供額AsyncResponse,通過一個(gè)異步線程來執(zhí)行查詢,在查詢完后,由這個(gè)異步線程完成對請求的響應(yīng)。

1、異步資源類
@Component
@Path("books")
@Produces({"application/json;charset=UTF-8", "application/javascript;charset=UTF-8", "text/javascript;charset=UTF-8"})
public class AsyncResource {
    private static final Logger log = LogManager.getLogger(AsyncResource.class);
    public static final long TIMEOUT = 120;
    final ExecutorService threadPool = Executors.newFixedThreadPool(10);

    @GET
    public void getAll(@Suspended final AsyncResponse asyncResponse) {
        //該方法用于定義回調(diào)
        configResponse(asyncResponse);
        final BatchRunner batchTask = new BatchRunner(asyncResponse);
        threadPool.submit(batchTask);
    }

    //回調(diào)方法  當(dāng)請你去處理完成之后,CompletionCallback實(shí)例的onComplete()方法將會被回調(diào),實(shí)現(xiàn)onComplete()方法,可以監(jiān)聽請求處理完成事件并實(shí)現(xiàn)相關(guān)業(yè)務(wù)流程。
    private void configResponse(final AsyncResponse asyncResponse) {
        asyncResponse.register((CompletionCallback) throwable -> {
            if (throwable == null) {
                log.info("CompletionCallback-onComplete: OK");
            } else {
                log.info("CompletionCallback-onComplete: ERROR: " + throwable.getMessage());
            }
        });

        //
        asyncResponse.register((ConnectionCallback) disconnected -> {
            //Status.GONE=410
            log.info("ConnectionCallback-onDisconnect");
            //當(dāng)請求--響應(yīng)模型的連接斷開的時(shí)候,CompletionCallback實(shí)例的onDisconnect()方法會被回調(diào)。實(shí)現(xiàn)這個(gè)方法可以監(jiān)聽連接斷開事件并實(shí)現(xiàn)相關(guān)業(yè)務(wù),比如主動(dòng)喚醒AsyncRespurce實(shí)例并設(shè)置狀態(tài)碼HTTP為410、客戶端請求資源不可用(Response.status(Response.Status.GONE)來完成響應(yīng)工作。
            disconnected.resume(Response.status(Response.Status.GONE).entity("disconnect!").build());
        });

        asyncResponse.setTimeoutHandler(new TimeoutHandler() {
        
        //TimeoutHandler是JAX-RS2定義的超時(shí)處理接口,用于處理異步響應(yīng)類超時(shí)事件,當(dāng)預(yù)期的超時(shí)時(shí)間到達(dá)之后,TimeoutHandler實(shí)例的handleTimeout()方法就會被調(diào)用。實(shí)現(xiàn)這個(gè)方法可以監(jiān)聽超時(shí)時(shí)間并處理相關(guān)業(yè)務(wù)。  并設(shè)置狀態(tài)碼為503、服務(wù)器端服務(wù)不可用(Response.Status.SERVICE_UNAVAILABLE)  TimeoutHandler的實(shí)現(xiàn)可以作為AsyncResource的setTimeoutHandler()方法的參數(shù)來配置。AsyncResource的setTimeout()方法用于設(shè)置超時(shí)時(shí)間,默認(rèn)永不超時(shí)。
            @Override
            public void handleTimeout(AsyncResponse asyncResponse) {
                //Status.SERVICE_UNAVAILABLE=503
                log.info("TIMEOUT");
                asyncResponse.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE).entity("Operation time out.").build());
            }
        });
        asyncResponse.setTimeout(TIMEOUT, TimeUnit.SECONDS);
    }

    class BatchRunner implements Runnable {
        private final AsyncResponse response;

        public BatchRunner(AsyncResponse asyncResponse) {
            this.response = asyncResponse;
        }

        @Override
        public void run() {
            try {
                Books books = doBatch();
                response.resume(books);
            } catch (InterruptedException e) {
                log.error(e);
            }
        }

        private Books doBatch() throws InterruptedException {
            Books books = new Books();
            for (int i = 0; i < 10; i++) {
                Thread.sleep(500);
                Book book = new Book(i + 10000l, "Java RESTful Web Services", "華章");
                log.debug(book);
                books.getBookList().add(book);
            }
            return books;
        }
    }
}
  • 1、測試方法:
    @Test
    public void testAsync() throws InterruptedException, ExecutionException {
        final Invocation.Builder request = target("http://localhost:" + this.port + "/books").request();
        final AsyncInvoker async = request.async();
        //客戶端試用AsyncInvoker的get()方法提交異步請求.該方法返回Future接口的實(shí)例,客戶端線程可以以非阻塞的方法處理其他業(yè)務(wù)流程,然后調(diào)用Future的get()方法來獲取服務(wù)器處理結(jié)果。
        final Future<Books> responseFuture =
    async.get(Books.class);
        long beginTime = System.currentTimeMillis();
        try {
            Books result = responseFuture.get(AsyncResource.TIMEOUT + 1, TimeUnit.SECONDS);
            log.debug("Testing result size = {}", result.getBookList().size());
            //如果在指定時(shí)間內(nèi)服務(wù)器沒有響應(yīng),將會報(bào)TimeoutException異常,我們可以捕獲這個(gè)異常并且實(shí)現(xiàn)超時(shí)處理。
        } catch (TimeoutException e) {
            log.debug("Fail to request asynchronously", e);
        } finally {
            log.debug("Elapsed time = {}", System.currentTimeMillis() - beginTime);
        }
    }

  • 2、回調(diào)方法
  @Test
    public void testAsyncCallBack() throws InterruptedException, ExecutionException {
        final AsyncInvoker async = target("http://localhost:" + this.port + "/books").request().async();
        final Future<Books> responseFuture = async.get(new InvocationCallback<Books>() {
            //處理REST回調(diào)成功的方法
            @Override
            public void completed(Books result) {
                log.debug("On Completed: " + result.getBookList().size());
            }
            //處理REST回調(diào)失敗的方法
            @Override
            public void failed(Throwable throwable) {
                log.debug("On Failed: " + throwable.getMessage());
                throwable.printStackTrace();
            }
        });
        log.debug("First response time::" + System.currentTimeMillis());
        try {
            responseFuture.get(AsyncResource.TIMEOUT + 1, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            log.debug("", e);
        } finally {
            log.debug("Second response time::" + System.currentTimeMillis());
        }
    }
  • 3、測試結(jié)果
    {
        "book":{
            {
                "bookId": 10000,
                "bookName": "JAVA RESTful Web Service",
                "publisher": "華章"
            },
             {
                "bookId": 10001,
                "bookName": "JAVA RESTful Web Service",
                "publisher": "華章"
            },
             {
                "bookId": 100002,
                "bookName": "JAVA RESTful Web Service",
                "publisher": "華章"
            },
             {
                "bookId": 10003,
                "bookName": "JAVA RESTful Web Service",
                "publisher": "華章"
            },
             {
                "bookId": 10004,
                "bookName": "JAVA RESTful Web Service",
                "publisher": "華章"
            },
             {
                "bookId": 10005,
                "bookName": "JAVA RESTful Web Service",
                "publisher": "華章"
            }
    
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • 前言 AOP(Aspect oriented Programming,面向切面編程),其實(shí)現(xiàn)原理就是代理被調(diào)用的方...
    菜鳥_一枚閱讀 1,008評論 0 0
  • Servlet過濾器是 Servlet 程序的一種特殊用法,主要用來完成一些通用的操作,如編碼的過濾、判斷用戶的登...
    重山楊閱讀 1,349評論 0 12
  • 閉上眼睛,伸開手臂,感受春風(fēng)拂面帶來的丁香氣息,感受身上陽光暖暖流淌的韻律,感受青草與泥土蘇醒的味道,感受內(nèi)心深處...
    3分鐘心理學(xué)閱讀 1,384評論 0 1
  • 那個(gè)時(shí)候,他和梅還沒有分開。他們兩個(gè)人一起應(yīng)聘到一家星級酒店上班。第一天上班點(diǎn)到的時(shí)候,他聽到了雪的名字。他這個(gè)人...
    鄭子陵閱讀 341評論 3 2

友情鏈接更多精彩內(nèi)容