Spring boot+websocket 服务端实现如何实现向多个不同用户定时推送不同数据,要定时推送的,急急急

博客访问: 642356
博文数量: 160
注册时间:
认证徽章:
修缘(需要交流的请加我的QQ )
ITPUB论坛APP
ITPUB论坛APP
APP发帖 享双倍积分
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: 开源技术
一:代码结构
二: pom.xml文件
三: web.xml文件
四: spring-servlet.xml文件
五:&WebSocketConfig.java文件
package com.gemdale.gmap.message.dispatch.
import java.util.L
import org.springframework.context.annotation.C
import org.springframework.messaging.converter.MessageC
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentR
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueH
import org.springframework.messaging.simp.config.ChannelR
import org.springframework.messaging.simp.config.MessageBrokerR
import org.springframework.util.AntPathM
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageB
import org.springframework.web.socket.config.annotation.StompEndpointR
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerC
import org.springframework.web.socket.config.annotation.WebSocketTransportR
import com.mon.util.ConfigureU
&* @author gengchong
&* @date 日 下午5:13:17
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
& & &* 服务器要监听的端口,message会从这里进来,要对这里加一个Handler
& & &* 这样在网页中就可以通过websocket连接上服务了
& & @Override
& & public void registerStompEndpoints(StompEndpointRegistry registry) {
& & & & registry.addEndpoint("/websocket")//websocket 端点
& & & & & & & & .setAllowedOrigins("*")
& & & & & & & & .setHandshakeHandler(new HandshakeHandler())
& & & & & & & & .addInterceptors(new HandshakeInterceptor()).withSockJS();
& & &* 消息传输参数配置&
& & @Override
& & public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
& & & & registry.setMessageSizeLimit(8192) //设置消息字节数大小
& & & & .setSendBufferSizeLimit(8192)//设置消息缓存大小
& & & & .setSendTimeLimit(10000); //设置消息发送时间限制毫秒
& & &* 输入通道参数设置
& & @Override
& & public void configureClientInboundChannel(ChannelRegistration registration) {
& & & & registration.taskExecutor().corePoolSize(4) //设置消息输入通道的线程池线程数
& & & & .maxPoolSize(8)//最大线程数
& & & & .keepAliveSeconds(60);//线程活动时间
& & &* 输出通道参数设置
& & @Override
& & public void configureClientOutboundChannel(ChannelRegistration registration) {
& & & & registration.taskExecutor().corePoolSize(4).maxPoolSize(8);
& & @Override
& & public void addArgumentResolvers(List argumentResolvers) {
& & @Override
& & public void addReturnValueHandlers(List returnValueHandlers) {
& & @Override
& & public boolean configureMessageConverters(List messageConverters) {
& & &* 配置broker
& & @Override
& & public void configureMessageBroker(MessageBrokerRegistry registry) {
& & & & registry.enableStompBrokerRelay("/topic") // 设置可以订阅的地址,也就是服务器可以发送的地址
& & & & & & & & .setRelayHost(ConfigureUtil.getProperty("BrokerUrl")).setRelayPort(Integer.valueOf(ConfigureUtil.getProperty("BrokerPort"))) // 设置broker的地址及端口号
& & & & & & & & .setSystemHeartbeatReceiveInterval(2000) // 设置心跳信息接收时间间隔
& & & & & & & & .setSystemHeartbeatSendInterval(2000); // 设置心跳信息发送时间间隔
& & & & registry.setApplicationDestinationPrefixes("/ws");
六:&HandshakeHandler.java文件
&* @author gengchong
&* @date 日 下午5:02:35
public class HandshakeHandler extends DefaultHandshakeHandler{
& & private static Logger logger = Logger.getLogger(HandshakeHandler.class);
六:&HandshakeInterceptor.java文件
public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor {
private static Logger logger = Logger.getLogger(HandshakeInterceptor.class);
& & @Override
& & public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
& & & & & & Map attributes) throws Exception {
& & & & ("============Before Handshake===========");
& & & & return super.beforeHandshake(request, response, wsHandler, attributes);
& & @Override
& & public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
& & & & & & Exception ex) {
& & & & ("============After Handshake==============");
& & & & super.afterHandshake(request, response, wsHandler, ex);
七:&GreetingController.java文件
public class GreetingController {
& & @MessageMapping("/hello")
& & @SendTo("/topic/gmapws")
& & public Greeting greeting(HelloMessage message)
& & & & try {
& & & & & & return new Greeting("Hello, "+message.getName() + " !");
& & & & catch (Exception e) {
& & & & & & e.printStackTrace();
八:&HelloMessage&.java文件
public class HelloMessage {
& & private S
& & &* @return the name
& & public String getName() {
& & &* @param name the name to set
& & public void setName(String name) {
& & & & this.name =
九:&Greeting.java文件
public class Greeting {
&private S
&public Greeting(String content)
& & &this.content=
&* @return the content
public String getContent() {
&* @param content the content to set
public void setContent(String content) {
& & this.content =
十:&index.html文件
阅读(8364) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。定时器之(三) - 定时器模块集群实现 - quartz与Spring的结合实现集群和非集群的定时器 - 为程序员服务
定时器之(三) - 定时器模块集群实现 - quartz与Spring的结合实现集群和非集群的定时器
quartz与Spring的结合实现集群和非集群的定时器
&spring.version&3.1.0.RELEASE&/spring.version&
&dependency&
&groupId&org.quartz-scheduler&/groupId&
&artifactId&quartz&/artifactId&
&version&1.8.6&/version&
&/dependency&
application-quartz-singleton.xml
&?xml version="1.0" encoding="UTF-8"?&
&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&
&bean id="SingetonTimerSample" class="com.freud.test.demo.SingletonTimerSample" /&
&!--配置调度具体执行的方法 --&
&bean id="SingetonJobSample"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&
&property name="targetObject" ref="SingetonTimerSample" /&
&property name="targetMethod" value="helloWorld" /&
&property name="concurrent" value="false" /&
&!--配置调度执行的触发的时间 --&
&bean id="SingletonTriggerSample" class="org.springframework.scheduling.quartz.CronTriggerBean"&
&property name="jobDetail" ref="SingetonJobSample" /&
&property name="cronExpression"&
&!-- 每5秒执行一次 --&
&value&0/5 * * * * ?&/value&
&/property&
&!-- quartz的调度工厂 调度工厂只能有一个,多个调度任务在list中添加 --&
&bean id="singletonSchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&
&property name="triggers"&
&!-- 所有的调度列表 --&
&ref local="SingletonTriggerSample" /&
&/property&
SingletonTimerSample.java
package com.freud.test.
import java.util.D
import org.springframework.beans.factory.annotation.A
* @author Freud
public class SingletonTimerSample
@Autowired
private TestService testS
public void helloWorld()
System.out.println(new Date() + "Hello Sample Singeton Timer!");
testService.index("Singleton");
catch (Exception e)
e.printStackTrace();
TestService.java
package com.freud.test.
import java.util.D
import org.springframework.stereotype.S
import org.springframework.transaction.annotation.T
* @author Freud
@Service("TestService")
public class TestService
public void index(String mode)
throws Exception
System.out.println(new Date() + "[" + mode + "]Test Quartz with Spring IOC");
集群方式(支持Spring注入)
application-quartz-cluster.xml
&?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-lazy-init="false"&
&!-- Quartz集群Schduler --&
&bean id="clusterQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&
&!-- Triggers集成 --&
&property name="triggers"&
&ref bean="ClusterTriggerSample" /&
&/property&
quartz配置文件路径--&
&property name="configLocation" value="classpath:conf/quartz.properties" /&
&property name="applicationContextSchedulerContextKey" value="applicationContext" /&
&property name="jobFactory"&
&bean class="com.freud.test.AutoWiringSpringBeanJobFactory" /&
&/property&
&bean id="ClusterTriggerSample" class="org.springframework.scheduling.quartz.CronTriggerBean"&
&property name="jobDetail" ref="ClusterJobDetailSample" /&
&!-- 每10秒执行一次 --&
&property name="cronExpression" value="0/10 * * * * ?" /&
&!-- Timer JobDetail, 基于JobDetailBean实例化Job Class,可持久化到数据库实现集群 --&
&bean id="ClusterJobDetailSample" class="org.springframework.scheduling.quartz.JobDetailBean"&
&property name="jobClass" value="com.freud.test.demo.ClusterTimerSample" /&
quartz.properties
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = ClusteredScheduler
org.quartz.scheduler.instanceId = AUTO
#org.quartz.scheduler.skipUpdateCheck = true
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.misfireThreshold = 6000
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 15000
org.quartz.jobStore.dataSource = freudDS
#==============================================================
#Non-Managed Configure Datasource
#==============================================================
org.quartz.dataSource.freudDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.freudDS.URL = jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=UTF-8
org.quartz.dataSource.freudDS.user = root
org.quartz.dataSource.freudDS.password = root
org.quartz.dataSource.freudDS.maxConnections = 100
ClusterTimerSample.java
package com.freud.test.
import java.util.D
import org.quartz.JobExecutionC
import org.quartz.JobExecutionE
import org.springframework.beans.factory.annotation.A
import org.springframework.scheduling.quartz.QuartzJobB
* @author Freud
public class ClusterTimerSample extends QuartzJobBean
@Autowired
private TestService testS
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException
System.out.println(new Date() + "Hello Cluster Timer sample!");
testService.index("cluster");
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();
AutoWiringSpringBeanJobFactory.java
package com.freud.
import org.quartz.spi.TriggerFiredB
import org.springframework.beans.factory.config.AutowireCapableBeanF
import org.springframework.context.ApplicationC
import org.springframework.context.ApplicationContextA
import org.springframework.scheduling.quartz.SpringBeanJobF
* @author Freud
public class AutoWiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware
private transient AutowireCapableBeanFactory beanF
public void setApplicationContext(final ApplicationContext context)
beanFactory = context.getAutowireCapableBeanFactory();
protected Object createJobInstance(final TriggerFiredBundle bundle)
throws Exception
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
最后把application-quartz-cluster.xml和application-quartz-singleton.xml引入Spring的ApplicationContext.xml文件就可以了
遇到的问题
数据库版本问题,在Mysql5.6.21下运行集群一直不成功,程序未报任何错误,但是集群Job不会运行,而切到5.5.*下就可以了。
Quartz支持插件式的配置开发,而要使用的话,就需要使用如下的Jar包
&dependency&
&groupId&javax.transaction&/groupId&
&artifactId&jta&/artifactId&
&version&1.1&/version&
&/dependency&
&dependency&
&groupId&quartz&/groupId&
&artifactId&quartz&/artifactId&
&version&1.5.2&/version&
&/dependency&
只支持quartz 1.
Oracle JDK从5.0开始在某些条件(特别是多线程)下已经不建议使用Timer,而是使用
java.util.concurrent.ScheduledThreadPoolExecutor
Quartz集群中给机器命名的方式(InstanceIdGenerator)有三种,分别是
HostnameInstanceIdGenerator
SimpleInstanceIdGenerator
SystemPropertyInstanceIdGenerator
org.quartz.scheduler.instanceId = AUTO
配置为AUTO即使用默认的
SimpleInstanceIdGenerator
package org.quartz.
import java.net.InetA
import org.quartz.SchedulerE
import org.quartz.spi.InstanceIdG
* The default InstanceIdGenerator used by Quartz when instance id is to be
* automatically generated.
Instance id is of the form HOSTNAME + CURRENT_TIME.
* @see InstanceIdGenerator
* @see HostnameInstanceIdGenerator
public class SimpleInstanceIdGenerator implements InstanceIdGenerator {
public String generateInstanceId() throws SchedulerException {
return InetAddress.getLocalHost().getHostName() + System.currentTimeMillis();
} catch (Exception e) {
throw new SchedulerException("Couldn't get host name!", e);
但是在linux环境下,如果没有配置机器别名的话,就会在
InetAddress.getLocalHost().getHostName()
报错,解决方案有2步,先
看下机器名是否是乱码,如果不是,就在
/etc/hosts
文件中添加
.0.0.1 localhost ${hostname}
Stay hungry, stay foolish. 少年辛苦终身事,莫向光阴惰寸功。
原文地址:, 感谢原作者分享。
您可能感兴趣的代码Spring+Websocket实时推送消息跟HTTPservlet实现原理一样吗【java吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:612,997贴子:
Spring+Websocket实时推送消息跟HTTPservlet实现原理一样吗收藏
实现方式感觉差别很大 有没有demo求参考
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
1.业务环境
Spring Boot Websocket
2.业务流程
Host设置口令,创建聊天室。
Guest输入口令,进入房间。
3.问题描述
我们已经知道,SocketJS与Spring Boot Websocket通过endpoint建立连接,Stomp客户端通过这个连接可以订阅消息以及发送消息。
我们想要了解,在Spring Boot Websocket中,如何根据用户的需要,动态地新建频道,让用户订阅消息以及发送消息,以实现创建聊天室的功能?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
根据我的理解,应该是这么一个流程:
Host设置口令,点击创建服务器接收到请求,为该口令生成一个唯一的channel(例如:room-xxx),在渲染的页面里面将room-xxx渲染到页面里面,可以用input hidden之类的,返回页面Host浏览器接收到页面,然后通过stomp建立连接,订阅room-xxx
Guest这个和Host类似,区别只是服务器接收到口令后直接找到channel的名称,然后渲染到页面上,这样通过口令就可以使他们在一个聊天室里了
同步到新浪微博
分享到微博?
你好!看起来你挺喜欢这个内容,但是你还没有注册帐号。 当你创建了帐号,我们能准确地追踪你关注的问题,在有新答案或内容的时候收到网页和邮件通知。还能直接向作者咨询更多细节。如果上面的内容有帮助,记得点赞 (????)? 表示感谢。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
扫扫下载 Appspringmvc+websocket 简单实现消息即时推送
我的图书馆
springmvc+websocket 简单实现消息即时推送
1、HandshakeInterceptor 类实现握手,获取session中的登陆用户信息,用以区分WebSocketSession,对指定用户进行推送信息。可使用WebSocketSession对象获取对应的用户:session.getAttributes().get( "username" )public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor {& &
@Override& &
public boolean beforeHandshake( ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,Map&String, Object& attributes ) throws Exception {& & & & // 获取登录用户& & & & if ( request instanceof ServletServerHttpRequest ) {& & & & & & ServletServerHttpRequest servletRequest = (ServletServerHttpRequest)& & & & & & HttpSession session = servletRequest.getServletRequest().getSession( false );& & & & & & if ( session != null ) {& & & & & & & & // 使用userName区分WebSocketHandler,以便定向发送消息& & & & & & & & String username = (String) session.getAttribute( "username" );& & & & & & & & attributes.put( "username", username );& & & & & & }& & & & }& & & && & }& & @Override& & public void afterHandshake( ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,&Exception ex ) {& }}2、MyWebSocketHandler类:消息处理类,需要实现WebSocketHandler接口。定义静态变量users集合,记录当前登录用户的WebSocketSession信息。public class WebsocketEndPoint implements WebSocketHandler {& & private static final ArrayList&WebSocketSession&& & static {& & & & users = new ArrayList&WebSocketSession&();& & }& & @Override& & public void afterConnectionEstablished( WebSocketSession session ) throws Exception {& & & & users.add( session );& & & & String username = (String) session.getAttributes().get( "username" );& & & & System.out.println( username + "登陆成功" );& & }& & @Override& & public void handleMessage( WebSocketSession session, WebSocketMessage&?& message ) throws Exception {& & & & // TODO Auto-generated method stub& & & & System.out.println( "接收到用户:" + (String) session.getAttributes().get( "username" ) + " 发送的信息:"& & & & & & + message.getPayload().toString() );& & & & sendMessageToUser( "dave", new TextMessage( (String) session.getAttributes().get( "username" ) + "发送给您:"& & & & & & + message.getPayload() ) );& & }& & @Override& & public void handleTransportError( WebSocketSession session, Throwable exception ) throws Exception {& & & & // TODO Auto-generated method stub& & }& & @Override& & public void afterConnectionClosed( WebSocketSession session, CloseStatus closeStatus ) throws Exception {& & & & // TODO Auto-generated method stub& & }& & @Override& & public boolean supportsPartialMessages() {& & & & // TODO Auto-generated method stub& & & && & }& & public void sendMessageToUser( String username, TextMessage message ) throws IOException {& & & & for ( WebSocketSession user : users ) {& & & & & & if ( username.equals( user.getAttributes().get( "username" ) ) ) {& & & & & & & & user.sendMessage( message );& & & & & & }& & & & }& & }}3、springmvc配置文件: &bean id="websocket" class="com.websocket.websocket.MyWebSocketHandler"/& & &websocket:handlers&
& &&websocket:mapping path="/ws" handler="websocket"/& &
& &&websocket:handshake-interceptors& &
& &&bean class="com.websocket.websocket.HandshakeInterceptor"/& &
& &&/websocket:handshake-interceptors& & &/websocket:handlers&4、html页面: //判断当前浏览器是否支持websocket if('WebSocket' in window){ websocket = new WebSocket("ws://localhost:8080/wesocket/ws"); }else{ alert("non-support"); }
& //连接发生错误 websocket.onerror = function(){ alert("error connect"); }; //连接成功 websocket.onopen = function(){ alert("open connnect"); }; //接收到的消息 websocket.onmessage = function(event){ alert(event.data); }; //连接关闭 websocket.onclose = function(){ alert("close"); websocket.close(); };&
//发送消息&
$("#sendBtn").click(function(){&
websocket.send("我是admin用户");&
})资料参考网址:/winkey4986/p/5478332.html/posts/view/21
TA的最新馆藏[转]&[转]&

我要回帖

更多关于 sping boot websocket 的文章

 

随机推荐