Streaming Api vs ActionPoller:
Streaming api is much better concept than action poller since action poller will send a request depends on time interval you set (this example 15 sec) that is much load on server action.
a)Action Poller:
public with sharing class DF12PollerController {
// in this example, the 'now' member variable must be part of the
// page's viewstate, because I only want to display new accounts that
// have been created after the page was initially loaded. every time
// the page's actionPoller request comes in, the only way that the
// controller will know the exact page load time is to serialize a
// DateTime variable down to the browser.
private DateTime now { get; set; }
transient public List<Account> accounts { get; private set; }
public DF12PollerController() {
this.now = DateTime.now();
this.accounts = new List<Account>();
}
public void find() {
this.accounts = [
SELECT Id, Name, CreatedDate
FROM Account
WHERE CreatedDate >= :this.now
];
}
@isTest
private static void testFind() {
DF12PollerController con = new DF12PollerController();
System.assert(con.now != null, '\'now\' should not be null.');
System.assert(con.accounts.size() == 0, '\'accounts\' should have zero records.');
Account a = new Account(Name = 'Test Account');
insert a;
con.find();
System.debug(con.accounts);
System.assert(con.accounts.size() > 0, '\'accounts\' should have at least 1 record.');
}
}
<apex:page docType="html-5.0" showHeader="false" standardStylesheets="false" controller="DF12PollerController">
<apex:composition template="DF12Template">
<apex:define name="pageTitle">DF '12: <apex:actionPoller> Example</apex:define>
<apex:define name="header">
<h1 class="pageTitle"><apex:actionPoller> Example</h1>
</apex:define>
<apex:define name="body">
<h4>New Accounts</h4>
<apex:form >
<apex:outputPanel layout="block" id="target">
<apex:repeat value="{!accounts}" var="a">
<p class="newItemRow">
<span class="newItem">{!a.Name}</span>
<span class="timeStamp">at {!a.CreatedDate}</span>
</p>
</apex:repeat>
</apex:outputPanel>
<apex:actionPoller action="{!find}" rerender="target" interval="20"/>
</apex:form>
</apex:define>
</apex:composition>
</apex:page>
b)Streaming Api:
public with sharing class DF12StreamingAPIController {
public PageReference createPushTopicIfNecessary() {
List<PushTopic> pt = [
SELECT Id, Name
FROM PushTopic
WHERE Name = 'NewAccounts'
LIMIT 1
];
if (pt.size() == 0) {
pt.add(new PushTopic(
ApiVersion = 23.0,
Name = 'NewAccounts',
Description = 'Returns all new accounts.',
Query = 'SELECT Id, Name, CreatedDate FROM Account'
));
upsert pt;
}
return null;
}
@isTest
private static void testCreatePushTopicIfNecessary() {
List<PushTopic> pt = [SELECT Id, Name FROM PushTopic WHERE Name = 'NewAccounts' LIMIT 1];
DF12StreamingAPIController con = new DF12StreamingAPIController();
try {
con.createPushTopicIfNecessary();
} catch (DmlException x) {
System.assert(true, 'Should already be a push topic created for this');
}
}
}
<apex:page docType="html-5.0" controller="DF12StreamingAPIController" action="{!createPushTopicIfNecessary}" showHeader="false" standardStylesheets="false">
<apex:styleSheet value="{!$Resource.AdvVFWebinarCSS}"/>
<apex:composition template="DF12Template">
<apex:define name="pageTitle">DF '12: Streaming API Example</apex:define>
<apex:define name="header">
<h1 class="pageTitle">Streaming API Example</h1>
</apex:define>
<apex:define name="body">
<h4>New Accounts</h4>
<div id="target"></div>
<div class="cheatLinkSeparator">
<a class="cheatLink" href="{!$Page.DF12Poller}" title="Open apex:actionPoller Example">apex:actionPoller Example</a>
</div>
</apex:define>
<apex:define name="pageScripts">
<script src="{!URLFOR($Resource.streaming_api_js, 'cometd.js')}"></script>
<script src="{!URLFOR($Resource.streaming_api_js, 'jquery-1.5.1.js')}"></script>
<script src="{!URLFOR($Resource.streaming_api_js, 'json2.js')}"></script>
<script src="{!URLFOR($Resource.streaming_api_js, 'jquery.cometd.js')}"></script>
<script>
(function($) {
$(document).ready(function() {
// first, connect to the salesforce cometd endpoint
$.cometd.init({
url: location.protocol + "//" + location.hostname + "/cometd/23.0/",
requestHeaders: { Authorization : "OAuth {!$Api.Session_Id}" }
});
// second, subscribe to a PushTopic (i.e., a "channel")
$.cometd.subscribe("/topic/NewAccounts", function(message) {
$("#target").append(
"<p class='newItemRow'><span class='newItem'>" +
message.data.sobject.Name +
"</span><span class='timeStamp'>at " +
message.data.sobject.CreatedDate +
"</span></p>"
);
});
}) // end document.ready
})(jQuery)
</script>
</apex:define>
</apex:composition>
</apex:page>
No comments:
Post a Comment