如何更改套接字读取超时?

时间:2013-10-07 10:43:04

标签: java sockets timeout javamail

我在那里使用JavaMail API:

Properties prop = new Properties();
prop.put("mail.smtp.starttls.enable","true");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.connectiontimeout", "2000");
Session session = Session.getInstance(prop, null);
Transport transport = session.getTransport("smtp"); 
transport.connect()

我不直接使用Socket对象,那么如何更改套接字读取超时?


StackTrace:

org.apache.geronimo.javamail.transport.smtp.SMTPConnection.receiveLine(SMTPConnection.java:760)
org.apache.geronimo.javamail.transport.smtp.SMTPConnection.receiveLine(SMTPConnection.java:682)
org.apache.geronimo.javamail.transport.smtp.SMTPConnection.getReply(SMTPConnection.java:692)
org.apache.geronimo.javamail.transport.smtp.SMTPConnection.getWelcome(SMTPConnection.java:448)
org.apache.geronimo.javamail.transport.smtp.SMTPConnection.protocolConnect(SMTPConnection.java:143)
org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:165)
javax.mail.Service.connect(Service.java:274)
controller.RegisterController.isAuthorized(RegisterController.java:132)
controller.RegisterController.register(RegisterController.java:102)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.apache.el.parser.AstValue.invoke(AstValue.java:278)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
org.apache.myfaces.view.facelets.el.ContextAwareTagMethodExpression.invoke(ContextAwareTagMethodExpression.java:96)
org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:68)
javax.faces.component.UICommand.broadcast(UICommand.java:120)
javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:1028)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:286)
javax.faces.component.UIViewRoot._process(UIViewRoot.java:1375)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:38)
org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170)
org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:724)

3 个答案:

答案 0 :(得分:1)

设置mail.smtp.timeout属性。请参阅javadocs for the com.sun.mail.smtp package

答案 1 :(得分:0)

我通过更改到最新版本的JavaMail(到JavaMail 1.5)来解决我的问题。我在那里写到:http://openejb.979440.n4.nabble.com/Which-version-of-JavaMail-td4665285.html

谢谢大家的帮助,特别是比尔香农:)

答案 2 :(得分:-1)

以前我使用过JAVAMAIL API。我认为你想要因为连接超时错误运行时而增加套接字超时。但是这不是由于这个问题而是一个重要的学习是当我们需要执行更大的请求时任何我们不应该直接在主线程上使用它们的应用程序,应该创建一个异步子线程并执行相同的电子邮件请求,然后在获得结果后更新主线程。