In the previous article flex+java+blazeds built a project based on the implementation of Flex subscription, Java send message push function.
first, the operating environment: Flex4.6+jdk1.7+blazedz a version + Tomcat6
Note here that the use of the server is TOMCAT6, if the use of Tomcat7 still have problems, can not achieve push, on the Internet to check this problem, some people say that the change BLAZEDS4 version can be achieved, has not been implemented, has yet to be resolved.
Second, the Code
1. Java side
(1) New class person for forward and backward data transfer
Package Com.java;public class Person {private int age;private String name;public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
(2) New servlet-- HelloServlet . Java that is used to invoke the thread that sent the message
Package Com.java;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Flex.messaging.messagebroker;import Flex.messaging.messages.asyncmessage;import Flex.messaging.util.uuidutils;public class HelloServlet extends HttpServlet {private static final long Serialversionuid = 6331466335470329744l;//thread private static Feedthread Thread;publ IC HelloServlet () {super ();} public void Destroy () {Super.destroy ();} public void Init () throws Servletexception {Super.init ();} public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { String cmd = request.getparameter ("cmd"), if (Cmd.equals ("start")) {start ();} if (Cmd.equals ("Stop")) {Stop ();}} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Super.dopost (request, response);} Start listening public void StArt () {if (thread = = null) {thread = new feedthread (); Thread.Start ();} SYSTEM.OUT.PRINTLN ("Start!!!");} Stop listening public void Stop () {thread.running = False;thread = null;} Loop send message process public static class Feedthread extends Thread {public Boolean running = True;public void run () {//always cannot get Msgbro Ker,web.xml need to have Messagebrokerservlet configuration information messagebroker msgbroker = Messagebroker.getmessagebroker (null); String ClientID = Uuidutils.createuuid (); int i = 0;while (running) {person person = new person ();p erson.setage (i);p erson.s Etname (string.valueof (i)); System.out.println (i); Asyncmessage msg = new Asyncmessage (); Msg.setdestination ("Person-data-feed"); Msg.setheader (asyncmessage.subtopic_ Header_name, "person"); Msg.setclientid (ClientID); Msg.setmessageid (Uuidutils.createuuid ()); Msg.settimestamp ( System.currenttimemillis ()); Msg.setbody (person); Msgbroker.routemessagetoservice (msg, null); I++;try {Thread.Sleep (2000);} catch (Interruptedexception e) {}}}}}
2. Flex Side
(1) New Personclient.mxml application for subscribing to messages
<?xml version= "1.0" encoding= "Utf-8"? ><s:application xmlns:fx= "http://ns.adobe.com/mxml/2009" xmlns:s= " Library://ns.adobe.com/flex/spark "xmlns:mx=" library://ns.adobe.com/flex/mx "minwidth=" 955 "minHeight=" > <fx:script><! [Cdata[import mx.controls.alert;import Mx.messaging.channelset;import Mx.messaging.consumer;import Mx.messaging.channels.streamingamfchannel;import Mx.messaging.events.messageevent;import vo. person;protected function Button1_clickhandler (event:mouseevent): Void{var consumer:consumer=new consumer (); Consumer.destination= "Person-data-feed"; consumer.subtopic= "person"; Consumer.addeventlistener ( Messageevent.message,messagehandler) var url:string = "http://localhost:8080/FlexToJava/"; var Mystreamingamf: Streamingamfchannel = new Streamingamfchannel (url+ "MY-STREAMING-AMF", url+ "MESSAGEBROKER/STREAMINGAMF"); var channelset:channelset = new ChannelSet (); Channelset.addchannel (MYSTREAMINGAMF); consumer.channelset= ChannelSet ;//consumer.channelset = NewchaNnelset (["MY-STREAMING-AMF"]); Consumer.subscribe (); Start receiving alert.show ("Consumer initialization is complete!") "); }protected function MessageHandler (event:messageevent): Void{var person:person=event.message.body as Person;this.lb_ Age.text=person.age.tostring (); this.lb_name.text=person.name;}]] ></fx:Script><fx:Declarations><!--Place non-visual elements (such as services, value objects) here--></fx:declarations><s: Label id= "Lb_age" x= "113" y= "" text= "label"/><s:label id= "Lb_name" x= "113" y= "Bayi" text= "label"/><s:button x= "164" y= "label=" subscription "click=" Button1_clickhandler (event)/></s:application>
(2) new person.as to receive the background person object
Package Vo{import mx.rpc.remoting.remoteobject;[ Remoteclass (alias= "Com.java.Person")] //For and Java background class conversion alias is the namespace of the Java class, otherwise cannot be converted [Bindable]public class person{ Public Function person () {}public var age:int;public var name:string;}}
3. configuration file
(1) Webroot\web-inf\flex\messaging-config.xml
Add the following section of code
<destination id= "Person-data-feed" > <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>
(2) Webroot\web-inf\flex\services-config.xml
Add the following section of code
<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 "> </endpoint> <properties> <id Le-timeout-minutes>0</idle-timeout-minutes> <max-streaming-clients>100</max-streaming-clien Ts> <server-to-client-heartbeat-millis>5000</server-to-client-heartbeat-millis> & lt;user-agent-settings> <user-agent match-on= "MSIE" kickstart-bytes= "2048" max-streaming-connections-per-session= "8"/> <user-agent match-on= "Firefox" kickstart-bytes= "2048" max-streaming-connections-per-session= "8"/> </user-agent-settings> </properties></chann El-definition>
(3) Webroot\web-inf\web.xml
The servlet may have been established, if not, add the following code
<servlet><description>this is the description of my EE component</description><display-name >this is the display name of my EE component</display-name><servlet-name>helloservlet</ servlet-name><servlet-class>com.java.helloservlet</servlet-class></servlet>< servlet-mapping><servlet-name>helloservlet</servlet-name><url-pattern>/helloservlet</ Url-pattern></servlet-mapping>
4. Run
(1) to start a thread in a Servlet
Http://localhost:8080/FlexToJava/HelloServlet?cmd=start
( 2 ) launch Flex page
Click Subscribe to display the results of the background dynamically
(3) to close a thread in a Servlet
Http://localhost:8080/FlexToJava/HelloServlet?cmd=stop
Demo Finished!
Java+flex+blaze push