`
lch_2010
  • 浏览: 35185 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Spring BlazeDS Integration Flex4 Java 后台数据推送

    博客分类:
  • java
阅读更多

web.xml 

 

<servlet>
	<servlet-name>flex</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    	<servlet-name>flex</servlet-name>
    	<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>

 

 spring 

 

 

<flex:message-broker>
		<flex:message-service
			default-channels="my-streaming-amf,my-longpolling-amf,my-polling-amf" />
</flex:message-broker>
<flex:message-destination id="simple-feed" />

<!-- Pojo used to start and stop the data feed that pushes data in the 'simple-feed' destination -->
<bean id="simpleFeedStarter" class="SimpleFeed 类路径">
	<constructor-arg ref="defaultMessageTemplate" />
	<flex:remoting-destination />
</bean>

java:

 

import java.util.Random;

import org.springframework.flex.messaging.MessageTemplate;

public class SimpleFeed {

    private static FeedThread thread;

    private final MessageTemplate template;

    public SimpleFeed(MessageTemplate template) {
        this.template = template;
    }

    public void start() {
        if (thread == null) {
            thread = new FeedThread(this.template);
            thread.start();
        }
    }

    public void stop() {
        thread.running = false;
        thread = null;
    }

    public static class FeedThread extends Thread {

        public boolean running = false;

        private final MessageTemplate template;

        public FeedThread(MessageTemplate template) {
            this.template = template;
        }

        @Override
        public void run() {
            this.running = true;
            Random random = new Random();
            double initialValue = 35;
            double currentValue = 35;
            double maxChange = initialValue * 0.005;

            while (this.running) {
                double change = maxChange - random.nextDouble() * maxChange * 2;
                double newValue = currentValue + change;

                if (currentValue < initialValue + initialValue * 0.15 && currentValue > initialValue - initialValue * 0.15) {
                    currentValue = newValue;
                } else {
                    currentValue -= change;
                }

                this.template.send("simple-feed", new Double(currentValue));

                System.out.println("" + currentValue);

                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }

            }
        }
    }

}

 flex:

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="20" backgroundColor="#FFFFFF">
	
	<mx:Script>
		<![CDATA[
		
			import mx.messaging.messages.IMessage;
			
			private function messageHandler(message:IMessage):void
			{
				pushedValue.text = ""+ message.body;	
			}
			
		]]>
	</mx:Script>
	
	<mx:ChannelSet id="cs">
		<!-- <mx:StreamingAMFChannel url="http://localhost:8080/testdrive/messagebroker/streamingamf"/> -->
		<mx:AMFChannel url="http://localhost:8080/testdrive/messagebroker/amflongpolling"/>
		<mx:AMFChannel url="http://localhost:8080/testdrive/messagebroker/amfpolling"/>
	</mx:ChannelSet>
	
	<mx:Consumer id="consumer" destination="simple-feed" channelSet="{cs}" 
		message="messageHandler(event.message)"/>

	<mx:TextInput id="pushedValue" width="250"/>

	<mx:Button label="Subscribe" click="consumer.subscribe()" enabled="{!consumer.subscribed}"/>
	<mx:Button label="Unsubscribe" click="consumer.unsubscribe()" enabled="{consumer.subscribed}"/>

		
</mx:Application>
 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics