Sie sind auf Seite 1von 3

Te ch n i c a l W h i t e Pa p e r S e r i e s

Server Push with Flex & J2EE


This is one of the features which have been done for one of Xpflow releases and quite exciting in nature which has prompted me to
write this article. There are cases where the data being displayed in textual or graphical format changes over-time and we would like
the users to be aware about it even without having to refresh the screen.
Typical applications: Email – when we get a new email - we want our selves to be notified about it automatically. Also if we want to
develop a monitoring application where the status of network, hardware etc which needs to be monitored at the real time, Stock
market observers, viewing cricket scores and many more.
What is wrong in refreshing the screen?
User does not know where there is a data and user will have to regularly press the refresh button – this is what we call busy wait –
which means we are taking time for the user but still being at wait stage – i.e. unnecessary usage of a persons time. Also if the UI is
image rich or the page refresh is time consuming then that aspect gets magnified.
Pull/poll Vs Push.
Now there are options to either poll the backend services at a regular interval or let the server push the content whenever available.
Whats wrong in a poll ?
It’s a busy wait - consumes more CPU cycle from client machine. (http://en.wikipedia.org/wiki/Busy_waiting)
Alternate to Flex:
Without using Flex what are the options we have to develop a page which refreshes automatically with revised contents?
Well .. there is nothing like Push in HTML but i.e. we can't "push" anything to the browser.
Option 1: We can write applets or other client-resident programs that refresh, poll, monitor, listen to a server.
Option 2: Using HTTP META tag for with a dynamic page: like JSP or ASP and have that page re-render the results.
This way the browser generates a request every 10 sec say. Thus we can write a small jsp which shows the Cricket score and have the
meta tag defined – by virtue of which the page refreshes regularly and shows the updated score.
When the whole page should not be refreshed: We can also use frames in HTML page and have the HTML frame point to a dynamic
server-side component like a JSP page which in turn can be opened in a new page with updated content.
How internally data services work? Does it take a lot of network bandwidth?
Flex Data Services offers a number of data transport channels for communicating between a Flex client and the Flex Data Services
server. These channels include RTMP(s), AMF(s) and HTTP(s). Out of which data push is done over RTMP.

http://en.wikipedia.org/wiki/Real_Time_Messaging_Protocol

So this means its not a pull mechanism from the browser.

TM
Innovate. Execute.
www.eforceglobal.com
Te ch n i c a l W h i t e Pa p e r S e r i e s

Architecture:

Browser

Messaging
Flex
ActionScript Other MXML/AS
App(SWF) Libraries

J2EE Container

Flex Message Servlet

Flex Server Side


Message Publisher
Libraries(Java)

Above diagram is the conceptual representation of Server Push Architecture. This means the on client side a set of libraries reside and
talks to Flex Servlet which internally performs the connection between the server and the client open and performs the data push from
server to client without having to perform any client side refresh.
Code:
Here is a scenario where user has a list of tasks which he needs to work on and on run-time he needs to ge notified about any new
task arrivals.
Message publisher in this can tells which all user ids have received a new item in the list of pending tasks to the Flex side action
script components.
Message Publisher:
public static void refreshDashBoard(final String mailids)
{
final MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
final String clientID = UUIDUtils.createUUID(false);
final AsyncMessage msg = new AsyncMessage();
msg.setDestination("feed");
msg.setClientId(clientID);
msg.setMessageId(UUIDUtils.createUUID(false));
msg.setTimestamp(System.currentTimeMillis());
msg.setBody(mailids);
msgBroker.routeMessageToService(msg, null);
}

2
Te ch n i c a l W h i t e Pa p e r S e r i e s

Messaging-config.xml: This stores configuration of the Message Queue.


<destination id="feed">
<properties>
<network>
<session-timeout>0</session-timeout>
</network>
<server>
<max-cache-size>1000</max-cache-size>
<message-time-to-live>0</message-time-to-live>
<durable>false</durable>
</server>
</properties>
<channels>
<channel ref="my-rtmp"/>
</channels>
</destination>
In Flex side: A message consumer has been kept which once receives this message takes an action.
When a new message arrived a mssageHandler method gets called which makes a synchronous call to fetch the data.
private function messageHandler(message:IMessage):void
{
if(chekForEmailIds(message.body+"",model.globalvalues.loggedUserEmail))
{
var dbdel:DashBoardDelegate = dbdel = new DashBoardDelegate();
dbdel.getDashBoardData();
}
}
The component of Flex which actually performs the messaging: (from web.xml)
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<display-name>MessageBrokerServlet</display-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<init-param>
<param-name>flex.write.path</param-name>
<param-value>/WEB-INF/flex</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Das könnte Ihnen auch gefallen