1.整个项目框架
2.项目中的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gerrydevstory</groupId>
<artifactId>stockticker</artifactId>
<name>stockticker</name>
<packaging>war</packaging>
<version>1.0</version>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.0.2.RELEASE</org.springframework-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.2</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.项目中的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.servlet-context.xml配置文件
关于websocket的配置,建议是放在spring配置文件的root文件里,不然容易出现一些不必要的问题
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/websockethttp://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.gerrydevstory.stockticker" />
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/ws">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>
</beans>
5.Stock实体
package com.gerrydevstory.stockticker;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Stock implements Serializable {
private static final long serialVersionUID = 1L;
private String code = "";
private double price = 0.0;
private Date time = new Date();
public Stock() {
}
public Stock(String code, double price) {
this.code = code;
this.price = price;
}
private DateFormat df = new SimpleDateFormat("dd MMM yyyy, HH:mm:ss");
public String getTimeStr() {
return df.format(time);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
@Override
public String toString() {
return "Stock [code=" + code + ", price=" + price + ", time=" + getTimeStr() + "]";
}
}
6.home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Stock Ticker</title>
</head>
<body>
<h1>Stock Ticker</h1>
<table>
<thead><tr><th>Code</th><th>Price</th><th>Time</th></tr></thead>
<tbody id="price"></tbody>
</table>
<p class="new">
Code: <input type="text" class="code"/>
Price: <input type="text" class="price"/>
<button class="add">Add</button>
<button class="remove-all">Remove All</button>
</p>
<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
<script src="/stockticker/resources/stomp.js"></script>
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
//Create stomp client over sockJS protocol
var socket = new SockJS("http://127.0.0.1:8080/stockticker/ws");
var stompClient = Stomp.over(socket);
// Render price data from server into HTML, registered as callback
// when subscribing to price topic
function renderPrice(frame) {
var prices = JSON.parse(frame.body);
$('#price').empty();
for(var i in prices) {
var price = prices[i];
$('#price').append(
$('<tr>').append(
$('<td>').html(price.code),
$('<td>').html(price.price.toFixed(2)),
$('<td>').html(price.timeStr)
)
);
}
}
// Callback function to be called when stomp client is connected to server
var connectCallback = function() {
stompClient.subscribe('/topic/price', renderPrice);
};
// Callback function to be called when stomp client could not connect to server
var errorCallback = function(error) {
alert(error.headers.message);
};
// Connect to server via websocket
stompClient.connect("guest", "guest", connectCallback, errorCallback);
// Register handler for add button
$(document).ready(function() {
$('.add').click(function(e){
e.preventDefault();
var code = $('.new .code').val();
var price = Number($('.new .price').val());
var jsonstr = JSON.stringify({ 'code': code, 'price': price });
stompClient.send("/app/addStock", {}, jsonstr);
return false;
});
});
// Register handler for remove all button
$(document).ready(function() {
$('.remove-all').click(function(e) {
e.preventDefault();
stompClient.send("/app/removeAllStocks");
return false;
});
});
</script>
</body>
</html>
7.HomeController
package com.gerrydevstory.stockticker;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@Autowired private SimpMessagingTemplate template;
private TaskScheduler scheduler = new ConcurrentTaskScheduler();
private List<Stock> stockPrices = new ArrayList<Stock>();
private Random rand = new Random(System.currentTimeMillis());
/**
* Iterates stock list, update the price by randomly choosing a positive
* or negative percentage, then broadcast it to all subscribing clients
*/
private void updatePriceAndBroadcast() {
for(Stock stock : stockPrices) {
double chgPct = rand.nextDouble() * 5.0;
if(rand.nextInt(2) == 1) chgPct = -chgPct;
stock.setPrice(stock.getPrice() + (chgPct / 100.0 * stock.getPrice()));
stock.setTime(new Date());
}
System.out.println("---------------"+stockPrices.size());
template.convertAndSend("/topic/price", stockPrices);
System.out.println("******************");
}
/**
* Invoked after bean creation is complete, this method will schedule
* updatePriceAndBroacast every 1 second
*/
@PostConstruct
private void broadcastTimePeriodically() {
scheduler.scheduleAtFixedRate(new Runnable() {
@Override public void run() {
updatePriceAndBroadcast();
}
}, 100);
}
/**
* Handler to add one stock
*/
@MessageMapping("/addStock")
public void addStock(Stock stock) throws Exception {
stockPrices.add(stock);
updatePriceAndBroadcast();
}
/**
* Handler to remove all stocks
*/
@MessageMapping("/removeAllStocks")
public void removeAllStocks() {
stockPrices.clear();
updatePriceAndBroadcast();
}
/**
* Serve the main page
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
return "home";
}
}
8.效果展示