【问题标题】:Pubnub Not Working On Google App Engine (GAE)Pubnub 无法在 Google App Engine (GAE) 上运行
【发布时间】: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


【解决方案1】:

GAE 和长期运行的操作/连接

GAE 不允许您订阅,因为它是一个长时间运行的连接。而且 PubNub Java SDK v3 已停产,因此建议使用 v4 SDK(仍然无法解决 GAE/订阅问题)。应该可以在 Heroku 等其他应用程序主机上工作。

不熟悉 beanstalk 的细节,但只要它允许您的连接保持打开状态,它就应该可以工作。我确实阅读了有关使用 beanstalk 部署 Web 应用程序的简短说明,但是您不会在 Web 请求的上下文中使用 PubNub 订阅,因为 Web 请求是短暂的。您可以在 Web 请求中进行发布但不订阅。订阅可能会在您的客户端应用程序(浏览器 - 使用 JavaScript SDK)上执行。

更新

在 GAE 中使用 PubNub Java v4 SDK 时,请务必在 PNConfiguration 实例中启用它。 Does the Java SDK run on Google App Engine? KB article 提供了完整的详细信息。

【讨论】:

    猜你喜欢
    • 2013-03-31
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    相关资源
    最近更新 更多