Google app engine GAE使插座保持活动状态

Google app engine GAE使插座保持活动状态,google-app-engine,Google App Engine,我需要在AppEngine(服务器端)中创建一个TCP连接,并使其无限期(或至少30分钟)保持活动状态。我创建了一个后台线程,打开了一个java.net.Socket,并试图用一个包装好的BufferedReader读取一行。在大约3秒钟的不活动后,我得到以下异常: java.net.SocketException: Socket operation timed out: The API call remote_socket.Receive() took too long to respond

我需要在AppEngine(服务器端)中创建一个TCP连接,并使其无限期(或至少30分钟)保持活动状态。我创建了一个后台线程,打开了一个
java.net.Socket
,并试图用一个包装好的
BufferedReader
读取一行。在大约3秒钟的不活动后,我得到以下异常:

java.net.SocketException: Socket operation timed out: The API call remote_socket.Receive() took too long to respond and was cancelled.
我将在下面介绍我正在使用的代码的框架。任何帮助都将不胜感激(包括解决方法或关于GAE中套接字限制的信息)。谢谢

ThreadFactory tm=ThreadManager.backgroundThreadFactory();
thread=tm.newThread(newrunnable(){
@凌驾
公开募捐{
套接字=空;
试一试{
套接字=新套接字(“localhost”,8000);
插座。设置插座输出(0);
socket.setKeepAlive(true);
BufferedReader=新的BufferedReader(新的InputStreamReader(socket.getInputStream());
writer=newbufferedwriter(newoutputstreamwriter(socket.getOutputStream());
字符串in=reader.readLine();
//这里永远不会被处决
//...
}捕获(IOE异常){
e、 printStackTrace();
}最后{
// ... 
}
}
});
thread.start();

如果它是相关的,下面是一个用例:我有一个TCP服务器,用于在客户端使用嵌入式小程序。我想将Applet重新设计为一个webapp,但是服务器仍然可以正常工作,所以我不想改变它。我想编写一个简单的servlet,使TCP连接保持活动状态,并在TCP服务器和GWT客户端之间来回反馈消息。

由于安全原因,GAE中有很多限制,尽管GAE提供了许多随时可用的服务,这些服务需要像电子邮件一样使用套接字,xmpp等

首先,您的应用程序需要付费应用程序,意味着必须为使用GAE上的套接字启用计费

你可以喝一杯

以下是有关套接字限制的谷歌应用程序引擎文档的摘录:

App Engine支持套接字,无需导入任何 特殊的应用程序引擎库或添加任何特殊的应用程序引擎代码。 但是,您需要遵守某些限制和行为 使用套接字时请注意:

Sockets are available only for paid apps.
You cannot create a listen socket; you can only create outbound sockets.
java.net.URL is still configured to use the URL Fetch API; there is currently no way around this.
Most classes in javax.net.ssl are supported.
You can only use TCP or UDP; arbitrary protocols are not allowed.
You cannot bind to specific IP addresses or ports.
Port 25 (SMTP) is blocked; you can still use authenticated SMTP on the submission port 587.
Private, broadcast, multicast, and Google IP ranges (except those whitelisted below), are blocked:
    Google Public DNS: 8.8.8.8, 8.8.4.4, 2001:4860:4860::8888, 2001:4860:4860::8844 port 53
    Gmail SMTPS: smtp.gmail.com port 465 and 587
    Gmail POP3S: pop.gmail.com port 995
    Gmail IMAPS: imap.gmail.com port 993
Socket descriptors are associated with the App Engine app that created them and are non-transferable (cannot be used by other apps).
Sockets may be reclaimed after 2 minutes of inactivity; any socket operation keeps the socket alive for a further 2 minutes.
You cannot Select between multiple available sockets because that requires java.nio.SocketChannel which is not currently supported.)

那可能行不通。你的用例是什么?我们可以考虑其他谷歌云产品的替代方案。我添加了一个描述——见上面最后一段。这主要回答了我的问题。但是,您提供的代码似乎没有提供一种绕过3s超时的方法(请注意,我使用的是
setSoTimeout()
)。如果我错过了,请纠正我。我想你错过了。在线编号:76正在使用setSoTimeout(10000)。套接字将仅在这段时间内阻塞。如果超时过期,则会引发java.net.SocketTimeoutException,尽管套接字仍然有效。在进入阻塞操作之前,必须启用该选项才能生效。超时必须大于0。零超时被解释为无限超时。