1、demo.mxml:通过Consumer订阅,Producer发送消息
<mx:ChannelSet id="cs">
<mx:AMFChannel url="http://localhost:8080/JmsMessageDemo/messagebroker/amfpolling"/>
</mx:ChannelSet>
<mx:Consumer id="consumer" destination="chat-topic-jms" channelSet="{cs}"
message="messageHandler(event)" fault="messageFault(event)"/>
<mx:Producer id="producer" channelSet="{cs}"
destination="chat-topic-jms"
acknowledge="acknowledgeHandler(event)"
fault="faultHandler(event)"/>
注:这里指定channelset是因为我建的工程中services-config.xml中的配置为
<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>
运行程序时endpoint尽然变成http://{server.name}:{server.port}/webRoot/messagebroker/amfpolling(context.root应该是工程名字才对,这可能是我创建工程时设置的问题),所有要在consumer和producer中指定channelset
2、message-config.xml的配置(jms在本机):
<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" />
</adapters>
<default-channels>
<channel ref="my-polling-amf" />
</default-channels>
<destination id="chat-topic-jms">
<properties>
<jms>
<destination-type>Topic</destination-type>
<message-type>javax.jms.TextMessage</message-type>
<connection-factory>jms/flex/TopicConnectionFactory</connection-factory>
<destination-jndi-name>jms/topic/flex/simpletopic</destination-jndi-name>
<delivery-mode>NON_PERSISTENT</delivery-mode>
<message-priority>DEFAULT_PRIORITY</message-priority>
<preserve-jms-headers>"true"</preserve-jms-headers>
<acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
<!-- <connection-credentials username="guest" password="guest"/>-->
<max-producers>1</max-producers>
</jms>
</properties>
<adapter ref="jms"/>
</destination>
jms/flex/TopicConnectionFactory:jms的factory的JNDI名字
jms/topic/flex/simpletopic:jms的resource的JNDI名字
3、如果jms在远程,那么message-config.xml的配置:
<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" />
</adapters>
<default-channels>
<channel ref="my-polling-amf" />
</default-channels>
<destination id="chat-topic-jms">
<properties>
<jms>
<destination-type>Topic</destination-type>
<message-type>javax.jms.TextMessage</message-type>
<connection-factory>jms/flex/TopicConnectionFactory</connection-factory>
<destination-jndi-name>jms/topic/flex/simpletopic</destination-jndi-name>
<delivery-mode>NON_PERSISTENT</delivery-mode>
<message-priority>DEFAULT_PRIORITY</message-priority>
<preserve-jms-headers>"true"</preserve-jms-headers>
<acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
<!-- <connection-credentials username="guest" password="guest"/>-->
<max-producers>1</max-producers>
<!-- (Optional) JNDI environment. Use when using JMS on a remote JNDI server. -->
<initial-context-environment>
<property>
<name>java.naming.factory.initial</name>
<value>com.sun.enterprise.naming.SerialInitContextFactory</value>
</property>
<property>
<name>java.naming.factory.url.pkgs</name>
<value>com.sun.enterprise.naming</value>
</property>
<property>
<name>java.naming.factory.state</name>
<value>com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl</value>
</property>
<property>
<name>org.omg.CORBA.ORBInitialHost</name>
<value>{远程ip}</value>
</property>
<property>
<name>org.omg.CORBA.ORBInitialPort</name>
<value>3700</value>
</property>
</initial-context-environment>
</jms>
</properties>
<adapter ref="jms"/>
</destination>
节点initial-context-environment中的属性为JNDI lookup时需要设置的一些context属性(我用的是glassfish)