【发布时间】:2017-11-14 17:11:19
【问题描述】:
我正在 Google App Engine 上创建一个网络应用程序,它允许我订阅 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>
【问题讨论】:
-
@CraigConover 嗨。感谢您的评论。我看到 v3 已停产,我将开始使用 v4 SDK。我想问我是否可以在 Amazon Elastic Beanstalk 上托管应用程序?
-
@CraigConover 嗨。克雷格。感谢您的详细解释。非常感谢。
-
将我的 cmets 发布为官方答案。
标签: java mysql json google-app-engine pubnub