Flex与java Server Push数据

阅读更多

我根据网上Push实例进行整理下,可以实现Push功能 myeclipse8+flex4.6 

1.Flex 工程


	
		
	
	
		
	
	
		
		
		
	

 2.java 工程

package com.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Tick;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;

public class TickCacheServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;  
    private static FeedThread thread;  

	/**
	 * Constructor of the object.
	 */
	public TickCacheServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ 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(); } } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void start(){ if(thread == null){ thread = new FeedThread(); thread.start(); } System.out.println("thread start......"); } public void stop(){ thread.running = false; thread = null; System.out.println("thread stop......"); } public static class FeedThread extends Thread{ public boolean running = true; public void run(){ MessageBroker broker = MessageBroker.getMessageBroker(null); String clientID = UUIDUtils.createUUID(); int i = 0; while(running){ Tick tick = new Tick(); tick.setAskPrice(new BigDecimal("100")); tick.setBidPrice(new BigDecimal("100")); tick.setMidPrice(new BigDecimal("100")); tick.setTickTime(new Date()); tick.setSeqno(String.valueOf(i)); System.out.println(i); AsyncMessage msg = new AsyncMessage(); msg.setDestination("tick-data-feed"); msg.setHeader("DSSubtopic", "tick"); msg.setMessageId(UUIDUtils.createUUID()); msg.setTimestamp(System.currentTimeMillis()); msg.setBody(tick); broker.routeMessageToService(msg, null); i++; try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }

 

  • flex.rar (20.1 KB)
  • 下载次数: 2
  • java.rar (4.3 MB)
  • 下载次数: 2

你可能感兴趣的:(java,flex,push)