package com;
import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;
/**
* @文件名称: MessageBroker.java
* @文件描述: 实现消息推送
* @版权所有: (C)2009-2010
* @完成日期: 2010-1-11
* @公司:
* @作者: 王刚
* @版本: v1.0
*/
public class MessageFactory {
private String _DESTINATION_NAME = "feed";
public void sendMessage(String message,String subtopic) {
String clientID = UUIDUtils.createUUID(false);
MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
AsyncMessage msg = new AsyncMessage();
msg.setDestination(_DESTINATION_NAME);
msg.setHeader("DSSubtopic", subtopic);
msg.setClientId(clientID);
msg.setMessageId(UUIDUtils.createUUID());
msg.setTimestamp(System.currentTimeMillis());
msg.setBody(message);
msgBroker.routeMessageToService(msg, null);
}
public String get_DESTINATION_NAME() {
return _DESTINATION_NAME;
}
public void set_DESTINATION_NAME(String _destination_name) {
_DESTINATION_NAME = _destination_name;
}
}
/*
* Used to push data to the client.
*/
package com;
import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;
public class TestMessage extends Thread
{
public boolean running = true;
private static int _FEED_INTERVAL = 10000; // Interval in milliseconds to push the data.
private MessageFactory messageFactory;
public void run()
{
String clientID = UUIDUtils.createUUID(false);
while (running)
{
System.out.print("/消息测试77------");
messageFactory=new MessageFactory();
messageFactory.set_DESTINATION_NAME("feed");
messageFactory.sendMessage("群消息推!!001", "allids");
messageFactory.sendMessage("组消息推!!002", "groupids");
messageFactory.sendMessage("个人消息推!!003", "aloneids");
try
{
Thread.sleep(_FEED_INTERVAL);
}
catch (InterruptedException e)
{
}
}
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>BlazeDS</display-name>
<description>BlazeDS Application</description>
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<!-- MessageBroker Servlet -->
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<!-- for WebSphere deployment, please uncomment -->
<!--
<resource-ref>
<description>Flex Messaging WorkManager</description>
<res-ref-name>wm/MessagingWorkManager</res-ref-name>
<res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
-->
</web-app>
messaging-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<service id="message-service"
class="flex.messaging.services.MessageService">
<adapters>
<adapter-definition id="actionscript"
class="flex.messaging.services.messaging.adapters.ActionScriptAdapter"
default="true" />
<!--<adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter" default="true"/>-->
</adapters>
<destination id="security-check">
<properties>
<server>
<allow-subtopics>true</allow-subtopics>
<subtopic-separator>.</subtopic-separator>
</server>
</properties>
<channels>
<channel ref="my-polling-amf" />
<channel ref="my-streaming-amf" />
</channels>
</destination>
<destination id="feed">
<properties>
<network>
<session-timeout>0</session-timeout>
</network>
<server>
<allow-subtopics>true</allow-subtopics>
<subtopic-separator>.</subtopic-separator>
<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-polling-amf" />
<channel ref="my-streaming-amf" />
</channels>
</destination>
<default-channels>
<channel ref="my-polling-amf" />
</default-channels>
</service>
services-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<service-include file-path="remoting-config.xml" />
<service-include file-path="messaging-config.xml" />
<!--
<service-include file-path="remoting-config-demo.xml" />
<service-include file-path="proxy-config.xml" />
<service-include file-path="messaging-config.xml" />
-->
</services>
<security>
<login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
<!-- Uncomment the correct app server
<login-command class="flex.messaging.security.TomcatLoginCommand" server="JBoss">
<login-command class="flex.messaging.security.JRunLoginCommand" server="JRun"/>
<login-command class="flex.messaging.security.WeblogicLoginCommand" server="Weblogic"/>
<login-command class="flex.messaging.security.WebSphereLoginCommand" server="WebSphere"/>
-->
<!--
<security-constraint id="basic-read-access">
<auth-method>Basic</auth-method>
<roles>
<role>guests</role>
<role>accountants</role>
<role>employees</role>
<role>managers</role>
</roles>
</security-constraint>
-->
</security>
<channels>
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
<channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
<endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/>
<properties>
<add-no-cache-headers>false</add-no-cache-headers>
</properties>
</channel-definition>
<channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>true</polling-enabled>
<polling-interval-seconds>4</polling-interval-seconds>
</properties>
</channel-definition>
<channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf" class="flex.messaging.endpoints.StreamingAMFEndpoint"/>
<properties>
<idle-timeout-minutes>0</idle-timeout-minutes>
<max-streaming-clients>10</max-streaming-clients>
<server-to-client-heartbeat-millis>5000</server-to-client-heartbeat-millis>
<user-agent-settings>
<user-agent match-on="MSIE" kickstart-bytes="2048" max-streaming-connections-per-session="10"/>
<user-agent match-on="Firefox" kickstart-bytes="2048" max-streaming-connections-per-session="10"/>
</user-agent-settings>
</properties>
</channel-definition>
<!--
<channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
</channel-definition>
<channel-definition id="my-secure-http" class="mx.messaging.channels.SecureHTTPChannel">
<endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/httpsecure" class="flex.messaging.endpoints.SecureHTTPEndpoint"/>
<properties>
<add-no-cache-headers>false</add-no-cache-headers>
</properties>
</channel-definition>
-->
</channels>
<logging>
<target class="flex.messaging.log.ConsoleTarget" level="Error">
<properties>
<prefix>[BlazeDS] </prefix>
<includeDate>false</includeDate>
<includeTime>false</includeTime>
<includeLevel>false</includeLevel>
<includeCategory>false</includeCategory>
</properties>
<filters>
<pattern>Endpoint.*</pattern>
<pattern>Service.*</pattern>
<pattern>Configuration</pattern>
</filters>
</target>
</logging>
<system>
<redeploy>
<enabled>false</enabled>
<!--
<watch-interval>20</watch-interval>
<watch-file>{context.root}/WEB-INF/flex/services-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/proxy-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/remoting-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/messaging-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/data-management-config.xml</watch-file>
<touch-file>{context.root}/WEB-INF/web.xml</touch-file>
-->
</redeploy>
</system>
</services-config>
flex
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="init()"
height="150"
width="300"
fontSize="12">
<mx:Script>
<![CDATA[
import mx.messaging.FlexClient;
import mx.messaging.ChannelSet;
import mx.controls.Alert;
import mx.messaging.Consumer;
import mx.messaging.events.MessageEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;
private var allSubtipic:String="allids";
private var groupSubtipic:String="groupids";
private var aloneSubtipic:String="aloneids";
private var destination:String="feed";
private function init():void
{
if (Application.application.parameters.allSubtipic)
allSubtipic=Application.application.parameters.allSubtipic;
if (Application.application.parameters.groupSubtipic)
groupSubtipic=Application.application.parameters.groupSubtipic;
if (Application.application.parameters.aloneSubtipic)
aloneSubtipic=Application.application.parameters.aloneSubtipic;
if (Application.application.parameters.destination)
destination=Application.application.parameters.destination;
allSend();
groupSend();
aloneSend();
ExternalInterface.call("messageTest", "消息")
}
/**
* 群消息
* */
public function allSend():void
{
var consumer:Consumer=new Consumer();
consumer.destination=destination;
consumer.subtopic=allSubtipic;
consumer.addEventListener(MessageEvent.MESSAGE, taskSendMessageHandler);
consumer.subscribe();
}
public function groupSend():void
{
var consumer:Consumer=new Consumer();
consumer.destination=destination;
consumer.subtopic=groupSubtipic;
consumer.addEventListener(MessageEvent.MESSAGE, taskSendMessageHandler);
consumer.subscribe();
}
public function aloneSend():void
{
var consumer:Consumer=new Consumer();
consumer.destination=destination;
consumer.subtopic=aloneSubtipic;
consumer.addEventListener(MessageEvent.MESSAGE, taskSendMessageHandler);
consumer.subscribe();
}
private function taskSendMessageHandler(event:MessageEvent):void
{
var mess:String=event.message.body.toString();
Alert.show(mess);
trace(mess);
var s:String=ExternalInterface.call("messageTest", mess);
trace(s + "--------");
}
]]>
</mx:Script>
<mx:Button click="ExternalInterface.call('messageTest','消息')"/>
<mx:Label x="123.5"
y="10"
text="消息测试"
fontSize="12"/>
</mx:Application>