`
huobengle
  • 浏览: 863591 次
文章分类
社区版块
存档分类
最新评论

Getting started with BlazeDS

 
阅读更多

Getting started with BlazeDS


<!-- InstanceBeginEditable name="ArticleContent" -->

Building a messaging application

The BlazeDS Message Service provides a publish/subscribe infrastructure that allows your Flex application to publish messages and subscribe to a set of messaging destinations, enabling the development of real-time data push and collaborative applications.

Follow these steps to build a simple chat application that demonstrates the BlazeDS Message Service.

Step 1: Create the messaging destination

A messaging destination represents a topic of real time conversation that interested parties can subscribe (listen) to or contribute to by posting their own messages.

To define the simple chat destination for this application:

  1. In the blazeds-server project, open messaging-config.xml located in the Flex folder.
  2. Add a destination called tutorial-chat defined as follows:

    <destination id="tutorial-chat"/>

  3. Restart Tomcat.

A key element of a destination is the channel used to exchange data between the client and the server. Using BlazeDS, a messaging destination typically uses either a streaming or a polling channel.

  • Using a streaming channel, the server response is left open until the channel connection is closed, allowing the server to send incremental chunks of data to the client. Because HTTP connections are not duplex, a single streaming Action Message Format (AMF) or HTTP channel actually requires two browser HTTP connections in order to send data in both directions. One is needed for the streamed response from the server to the client that the channel hangs on to. A second transient connection, drawn from the browser pool, is needed only when data is sent to the server. This second transient connection is immediately released back to the browser’s connection pool after the data is sent.
  • A polling channel can be configured with a polling interval, or it can be set up to wait for data at the server-side if data is not immediately available (this approach is generally referred to as long polling). In either case, each poll response completes the request. Browser HTTP 1.1 connections are persistent by default, so the browser will likely recycle existing HTTP connections to send subsequent poll requests, which lowers the overhead for polling.

Notice that there is no need to explicitly define a channel for the tutorial-chat destination. When you do not specify channels at the destination level, the destination uses the default channels defined at the top of the messaging-config.xml file. In this case, the client will first try to connect to the message service using the “my-streaming-amf” channel. If a connection to the server cannot be established using that channel, the client will fall back to the “my-polling-amf” channel. Channels themselves are configured in services-config.xml.

Step 2: Create a Flex project

  1. In Eclipse, select File > New > Project…
  2. Expand the Flex Builder node, select Flex Project, and click Next.
  3. Enter “tutorial-chat” as the project name.
  4. Ensure Use default location is selected.
  5. Select Web Application as the application type.
  6. Select J2EE as the application server type.
  7. Select Use remote object access service.
  8. Deselect Create combined Java/Flex project using WTP.
  9. Click Next.
  10. Ensure the root folder for LiveCycle Data Services matches the root folder of your BlazeDS web application. The settings should look similar to these (you may need to adjust the exact folder name based on your own settings):

    Root Folder: C:/blazeds/tomcat/webapps/samples
    Root URL: http://localhost:8400/samples/
    Context Root: /samples

  11. Click Validate Configuration, and then Finish.

Step 3: Create the client application

In the newly created tutorial-chat project, open the main.mxml file located in the src folder, and implement the application as follows:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="consumer.subscribe()">

<mx:Script>
<![CDATA[import mx.messaging.events.MessageEvent;
import mx.messaging.messages.AsyncMessage;

private function send():void
{
var message:AsyncMessage = new AsyncMessage();
message.body.chatMessage = msg.text;
producer.send(message);
msg.text = "";
}

private function messageHandler(event:MessageEvent):void
{
log.text += event.message.body.chatMessage + "/n";
}

]]>
</mx:Script>

<mx:Producer id="producer" destination="tutorial-chat"/>
<mx:Consumer id="consumer" destination="tutorial-chat" message="messageHandler(event)"/>

<mx:Panel title="Chat" width="100%" height="100%">
<mx:TextArea id="log" width="100%" height="100%"/>
<mx:ControlBar>
<mx:TextInput id="msg" width="100%" enter="send()"/>
<mx:Button label="Send" click="send()"/>
</mx:ControlBar>
</mx:Panel>
</mx:Application>

Code highlights:
  • On the client side, the BlazeDS Message Service API provides two classes, Producer and Consumer, which you use to publish and subscribe to a destination, respectively.
  • To subscribe to a destination, you use the subscribe() method of the Consumer class.
  • When a message is published to a destination you subscribed to, the message event is triggered on the Consumer.

In this example, messages are published by Flex clients. The BlazeDS Message Service also provides a Java API that allows a server-side component to publish messages to a BlazeDS destination. A third option for exchanging messages between Flex and Java applications is to map destinations to Java Message Service (JMS) topics, enabling Flex clients to publish and subscribe to JMS topics.

Step 4: Run the application

  1. Click the Run icon in the Eclipse toolbar to start the application
  2. Open the same URL in another browser window to start a second instance of the chat application
  3. Type a message in one of the chat clients and click "Send": the message appears in the two chat clients
分享到:
评论

相关推荐

    BLAZEDS Developer Guide

    Getting started with BlazeDS

    BlazeDS开发者指南

    Part 1: Getting started with BlazeDS Chapter 1: Introduction to BlazeDS BlazeDS overview 2 BlazeDS features 3 Example BlazeDS applications 5 Chapter 2: Building and deploying BlazeDS applications ...

    BlazeDS,PureMVC

    BlazeDS通信到Java的PureMVC——Flex框架,BlazeDS的服务器配置比较麻烦,这里使用的是直接下载blazeds_turnkey_3-0-0-544.zip中的tomcat BlazeDS模板路径为: blazeds_turnkey_3-0-0-544.zip解压后路径\tomcat\...

    blazeds4.7官方下载

    BlazeDS是一个基于服务器的Java远程调用(remoting)和Web消息传递(messaging)技术,使得后台的Java应用程序和运行在浏览器上的Flex应用程序能够相互通信。

    Flex on Java MEAP Jul 2010

    Part 1: Getting started 1. Some Flex with your Java 2. Beginning with Java 3. Getting Rich with Flex 4. Connecting to Web Services Part 2: Strengthening the backend 5. BlazeDS remoting and ...

    blazeds 服务器war包

    blazeds.war blazeds

    blazeds4.0.1

    包含blazeds.war blazeds-spring.war 以及解压后的blazeds,blazeds-spring文件夹, 可直接拷贝到javaee项目下的WebRoot文件夹下

    Blazeds入门教程.doc

    Blazeds入门教程.doc

    Spring BlazeDS Integration Spring集成BlazeDS

    Spring BlazeDS Integration,Flex通过remoteObject与java通讯,附带例子 文档

    Flex Spring JAVA BLAZEDS整合

    Flex技术本身和Java就有很强的关联性,它提供了一个基于Eclipse的IDE和BlazeDS.BlazeDS是个基于服务端的Java远程调用和Web消息的一个开源的技术。有许多应用都是以Java为后端处理的。Flex用于前端。由于Java和Flex...

    BlazeDS入门:HTTP Service

    BlazeDS入门:HTTP Service BlazeDS入门:HTTP Service BlazeDS入门:HTTP Service

    blazeds4整合spring3

    blazeds4整合spring3 这是我新近研究的方法 与整合spring2.5以下的方法有不同了,(看过一些教程觉得不是很好,用起来不顺手) 最终参考了官方的sample,得到了这个不错的整合方法。 把要用blazeds公开给flex的java...

    blazeds和flex整合

    Using BlazeDS and Flex integrated。

    flex 工具 blazeds

    flex 工具 blazeds

    blazeDS war包及Flex结合blazeDS的入门程序

    blazeDS war包及Flex结合blazeDS的入门程序及操作步骤比较适合入门选手

    spring-blazeds结合项目源码

    blazeds与spring的结合使用项目源码

    Flex Blazeds入门教程

    Flex Blazeds入门教程 写的很不错的教程

    JAVA通过BlazeDS与FLEX通信

    JAVA通过BlazeDS与FLEX通信 BlazeDS.zip java项目,使用MYECLIPSE导入 FlexJavaServer.fxp FLEX项目,使用FLASHBUILDER4.5导入

    BlazeDS开发者指南中文版

    BlazeDS开发者指南中文版,很详细的描述了BlazeDs的基本原理,开发、配置过程。

    flex+blazeDS配置指南

    BlazeDS配置指南 tomcat安装 xml配置 flex与blazeDs

Global site tag (gtag.js) - Google Analytics