我正在Google App Engine上创建一个Web应用程序,该应用程序使我可以订阅Pubnub频道,接收消息并将这些消息存储到数据库中。 我已将代码上传到Google App Engine,但在我查看GAE日志时似乎无法正常工作,它没有更新。 我在Pubnub网站上的调试控制台上尝试后,数据库也未更新。 我很确定我的频道名称,订阅和发布密钥正确。 我从Pubnub Github Example获取代码。 希望这里的所有用户都能帮助我找到并指出问题。 我对Pubnub还是陌生的,随时可以提出建议。 非常感谢你。
我的代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.json.JSONArray;
import org.json.JSONObject;
import com.pubnub.api.Callback;
import com.pubnub.api.Pubnub;
import com.pubnub.api.PubnubException;
import com.yihwei95.model.appdata.AppData;
class Receiver implements Callback {
public boolean successCallback(String channel, Object message) {
final String JDBC_DRIVER = "com.mysql.jdbc.GoogleDriver";
final String DB_URL = "jdbc:google:mysql"
+ "://****"
+ ":asia-northeast1"
+ ":****/****?user=****";
Connection conn = null;
PreparedStatement prst = null;
try {
if (message instanceof JSONObject) {
JSONObject obj = (JSONObject) message;
AppData ad = new AppData();
String email = obj.getString("email");
ad.setEmail(email);
String password = obj.getString("password");
ad.setPassword(password);
String token = obj.getString("token");
ad.setToken(token);
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL);
String SQL = "INSERT INTO ****"
+ "(email, password, token)"
+ "VALUES(?, ? ,?)";
prst = conn.prepareStatement(SQL);
prst.setString(1, ad.getEmail());
prst.setString(2, ad.getPassword());
prst.setString(3, ad.getToken());
prst.executeUpdate();
prst.close();
conn.close();
//@SuppressWarnings("rawtypes")
//Iterator keys = obj.keys();
//while (keys.hasNext()) {
//System.out.print(obj.get(keys.next().toString())
//+ " ");
//}
//System.out.println();
}
else if (message instanceof String) {
String obj = (String) message;
//System.out.print(obj + " ");
//System.out.println();
}
else if (message instanceof JSONArray) {
JSONArray obj = (JSONArray) message;
//System.out.print(obj.toString() + " ");
//System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
// Continue Listening?
return true;
}
public void errorCallback(String channel, Object message) {
//System.err.println("Channel:" + channel + "-"
//+ message.toString());
}
public void connectCallback(String channel) {
//System.out.println("Connected to channel :" + channel);
//System.out.println("Waiting for message ...");
}
public void reconnectCallback(String channel) {
//System.out.println("Reconnected to channel :" + channel);
}
public void disconnectCallback(String channel) {
//System.out.println("Disconnected to channel :" + channel);
}
}
public class Testing{
static String subscribeKey = "****";
static String publishKey = "****";
static String channel = "****";
static Pubnub pubnub = null;
public Testing(){
pubnub = new Pubnub(publishKey, subscribeKey);
postAppData();
}
private static void postAppData(){
try{
pubnub.subscribe(channel, new Receiver());
}
catch(PubnubException pubnube){
pubnube.printStackTrace();
return;
}
}
}
二手JAR
还是应该修改Web.XML文件?
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>TestingGCP</servlet-name>
<servlet-class>com.yihwei95.TestingGCPServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestingGCP</servlet-name>
<url-pattern>/testinggcp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
GAE不允许您订阅,因为它是长期运行的连接。 并且PubNub Java SDK v3已停产,因此建议您使用v4 SDK(仍然无法解决GAE /订阅问题)。 应该在其他应用程序主机(如Heroku等)上运行。
不熟悉beantalk的细节,但只要它可以使您的连接保持打开状态,它就可以正常工作。 我确实阅读了有关使用beantalk部署Web应用程序的快速摘要,但是您不会在Web请求的上下文中使用PubNub订阅,因为Web请求是短暂的。 您可以在Web请求中进行发布,但不进行订阅。 订阅可能会在您的客户端应用程序(浏览器-使用JavaScript SDK)上执行。
在GAE中使用PubNub Java v4 SDK时 ,请确保在PNConfiguration实例中启用它。 Java SDK是否可以在Google App Engine上运行吗? KB文章 。