AngularJS 与jersey进行通讯备忘

DPA应用使用angular 的$http与Jersey的RESTFul接口进行ajax访问,之前一直比较顺畅,没出过什么问题。近日在**电信的计费库部署时发现,部分功能不能正常使用。
检查jersey端输出:

Apr 02, 2018 5:37:37 PM org.glassfish.jersey.server.ServerRuntime$Responder writeResponse
SEVERE: An I/O error has occurred while writing a response message entity to the container output stream.
org.glassfish.jersey.server.internal.process.MappableException: org.apache.catalina.connector.ClientAbortException: java.net.SocketException: Broken pipe (Write failed)
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:96
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1139
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:574
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:381
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:371
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:262
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267
at org.glassfish.jersey.internal.Errors.process(Errors.java:315
at org.glassfish.jersey.internal.Errors.process(Errors.java:297
at org.glassfish.jersey.internal.Errors.process(Errors.java:267
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220

能看到broken pipe,写入错误,ClientAbortException等字样…… 服务器端代码执行时间可能略长一些,前端angular可能有默认超时机制——这个我们之前没有考虑这一点,因为没出过这个问题。查看AngularJS文档,修改angular:

dpaModule.config(function($httpProvider) {
$httpProvider.interceptors.push(‘myHttpInterceptor’);
var spinnerFunction = function (data, headersGetter) {
$(‘#mydiv’).show(); return data; };
$httpProvider.defaults.transformRequest.push(spinnerFunction); $httpProvider.defaults.timeout = 10*60*1000; });

把默认超时时间设置为10分钟,各项功能这回正常了

Leave Comment