销售人员EMP连接器,一段时间后停止接收通知

时间:2017-02-24 06:56:51

标签: api salesforce streaming connector

我正在执行POC来检查Streaming API的稳定性,POC如下

程序1:订阅针对Account对象创建的pushtopic 计划2:创建,更新和每10分钟间隔后删除一条记录

这两个程序都保持运行超过12小时(留下过夜),之后我验证是否收到所有通知,并发现在一段时间后(在这种情况下,它差不多~2小时45分钟)没有通知收到了,我重复了两次这两种情况都会在一段时间后停止收到通知。

使用的测试代码

Streaming API客户端(使用EMP连接器)

public class SFPoc {

static Long count = 0L;
static Long Leadcount = 0L;

public static void main(String[] argv) throws Exception {
    String userName = "<user_name>";
    String password = "<pwd>";
    String pushTopicName = "/topic/AccountPT";
    String pushTopicNameLead = "/topic/Leadwhere";
    long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST;
    String securityToken = "<token>";

    BayeuxParameters custom = getBayeuxParamWithSpecifiedAPIVersion("37.0");
    BayeuxParameters params = null;
    try {
        params = login(userName, password +  securityToken, custom);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Consumer<Map<String, Object>> consumer = event -> System.out.println(String.format("Received:\n%s ** Recieved at %s, event count total %s", event, LocalDateTime.now() , ++count));
    Consumer<Map<String, Object>> consumerLead = event -> System.out.println(String.format("****** LEADS ***** Received:\n%s ** Recieved at %s, event count total %s", event, LocalDateTime.now() , ++Leadcount));

    EmpConnector connector = new EmpConnector(params);

    connector.start().get(10, TimeUnit.SECONDS);

    TopicSubscription subscription = connector.subscribe(pushTopicName, replayFrom, consumer).get(10, TimeUnit.SECONDS);
    TopicSubscription subscriptionLead = connector.subscribe(pushTopicNameLead, replayFrom, consumerLead).get(10, TimeUnit.SECONDS);

    System.out.println(String.format("Subscribed: %s", subscription));
    System.out.println(String.format("Subscribed: %s", subscriptionLead));
}

private static BayeuxParameters getBayeuxParamWithSpecifiedAPIVersion(String apiVersion) {
    BayeuxParameters params = new BayeuxParameters() {

        @Override
        public String version() {
            return apiVersion;
        }

        @Override
        public String bearerToken() {
            return null;
        }

    };
    return  params;
}

}

定期创建/更新/删除记录的代码以生成事件

import com.sforce.soap.enterprise.*;
import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Contact;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

import java.time.LocalDateTime;

public class SFDCDataAdjustment {

static final String USERNAME = "<username>";
static final String PASSWORD = "<pwd&securitytoken>";

static EnterpriseConnection connection;

static Long count = 0L;

public static void main(String[] args) {

    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(USERNAME);
    config.setPassword(PASSWORD);
    //config.setTraceMessage(true);

    try {

        connection = Connector.newConnection(config);

        // display some current settings
        System.out.println("Auth EndPoint: "+config.getAuthEndpoint());
        System.out.println("Service EndPoint: "+config.getServiceEndpoint());
        System.out.println("Username: "+config.getUsername());
        System.out.println("SessionId: "+config.getSessionId());

        // run the different examples

        while (true) {
            createAccounts();
            updateAccounts();
            deleteAccounts();

            Thread.sleep(1 * 10 * 60 * 1000);
        }


    } catch (ConnectionException e1) {
        e1.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

// queries and displays the 5 newest contacts
private static void queryContacts() {

    System.out.println("Querying for the 5 newest Contacts...");

    try {

        // query for the 5 newest contacts
        QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Account.Name " +
                "FROM Contact WHERE AccountId != NULL ORDER BY CreatedDate DESC LIMIT 5");
        if (queryResults.getSize() > 0) {
            for (int i=0;i<queryResults.getRecords().length;i++) {
                // cast the SObject to a strongly-typed Contact
                Contact c = (Contact)queryResults.getRecords()[i];
                System.out.println("Id: " + c.getId() + " - Name: "+c.getFirstName()+" "+
                        c.getLastName()+" - Account: "+c.getAccount().getName());
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

// create 5 test Accounts
private static void createAccounts() {

    System.out.println("Creating a new test Account...");
    Account[] records = new Account[1];

    try {

        // create 5 test accounts
        for (int i=0;i<1;i++) {
            Account a = new Account();
            a.setName("OptyAccount "+i);
            records[i] = a;
        }

        // create the records in Salesforce.com
        SaveResult[] saveResults = connection.create(records);

        // check the returned results for any errors
        for (int i=0; i< saveResults.length; i++) {
            if (saveResults[i].isSuccess()) {
                System.out.println(i+". Successfully created record - Id: " + saveResults[i].getId() + "At " + LocalDateTime.now());
                System.out.println("************Event Count************" + ++count);
            } else {
                Error[] errors = saveResults[i].getErrors();
                for (int j=0; j< errors.length; j++) {
                    System.out.println("ERROR creating record: " + errors[j].getMessage());
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

// updates the 5 newly created Accounts
private static void updateAccounts() {

    System.out.println("Update a new test Accounts...");
    Account[] records = new Account[1];

    try {

        QueryResult queryResults = connection.query("SELECT Id, Name FROM Account ORDER BY " +
                "CreatedDate DESC LIMIT 1");
        if (queryResults.getSize() > 0) {
            for (int i=0;i<queryResults.getRecords().length;i++) {
                // cast the SObject to a strongly-typed Account
                Account a = (Account)queryResults.getRecords()[i];
                System.out.println("Updating Id: " + a.getId() + " - Name: "+a.getName());
                // modify the name of the Account
                a.setName(a.getName()+" -- UPDATED");
                records[i] = a;
            }
        }

        // update the records in Salesforce.com
        SaveResult[] saveResults = connection.update(records);

        // check the returned results for any errors
        for (int i=0; i< saveResults.length; i++) {
            if (saveResults[i].isSuccess()) {
                System.out.println(i+". Successfully updated record - Id: " + saveResults[i].getId() + "At " + LocalDateTime.now());
                System.out.println("************Event Count************" + ++count);
            } else {
                Error[] errors = saveResults[i].getErrors();
                for (int j=0; j< errors.length; j++) {
                    System.out.println("ERROR updating record: " + errors[j].getMessage());
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

// delete the 5 newly created Account
private static void deleteAccounts() {

    System.out.println("Deleting  new test Accounts...");
    String[] ids = new String[1];

    try {

        QueryResult queryResults = connection.query("SELECT Id, Name FROM Account ORDER BY " +
                "CreatedDate DESC LIMIT 1");
        if (queryResults.getSize() > 0) {
            for (int i=0;i<queryResults.getRecords().length;i++) {
                // cast the SObject to a strongly-typed Account
                Account a = (Account)queryResults.getRecords()[i];
                // add the Account Id to the array to be deleted
                ids[i] = a.getId();
                System.out.println("Deleting Id: " + a.getId() + " - Name: "+a.getName());
            }
        }

        // delete the records in Salesforce.com by passing an array of Ids
        DeleteResult[] deleteResults = connection.delete(ids);

        // check the results for any errors
        for (int i=0; i< deleteResults.length; i++) {
            if (deleteResults[i].isSuccess()) {
                System.out.println(i+". Successfully deleted record - Id: " + deleteResults[i].getId() + "At " + LocalDateTime.now());
                System.out.println("************Event Count************" + ++count);
            } else {
                Error[] errors = deleteResults[i].getErrors();
                for (int j=0; j< errors.length; j++) {
                    System.out.println("ERROR deleting record: " + errors[j].getMessage());
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

进一步更新得到以下提到的错误,之后通知

2017-03-09T19:30:28.346 ERROR [com.salesforce.emp.connector.EmpConnector] - 连接失败,重新连接 org.cometd.common.TransportException:{httpCode = 503}     at org.cometd.client.transport.LongPollingTransport $ 2.onComplete(LongPollingTransport.java:278)     在org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:193)

此重新连接也发生了并且握手也发生但是错误似乎在重新订阅()EMP连接器似乎无法由于某种原因重新订阅

注意我正在使用&#34; resubscribe-on-disconnect&#34; EMP connetor的分支

2 个答案:

答案 0 :(得分:1)

我们已经确定在403案例中服务器端存在错误。 Streaming API使用会话路由cookie,此cookie定期到期。当它到期时,会话被路由到另一个服务器,并以403响应。在当前版本中,此403响应不包括连接建议,并且客户端不会尝试重新连接。这已得到修复,修复程序目前正在运行。我的理解是,这应该解决客户展示的重新连接问题。

答案 1 :(得分:0)

你能描述一下你的POC吗?你用过什么客户?你能分享POC结构和你正在做什么吗?

这有很多潜在的原因,但我无法理解客户,你正在尝试做什么,以及有关失败的更多信息。