STOMP协议介绍
STOMP,Streaming Text Orientated Message Protocol,是流文本定向消息协议,是一种为MOM(Message Oriented Middleware,面向消息的中间件)设计的简单文本协议。
它提供了一个可互操作的连接格式,允许STOMP客户端与任意STOMP消息代理(Broker)进行交互,类似于OpenWire(一种二进制协议)。
由于其设计简单,很容易开发客户端,因此在多种语言和多种平台上得到广泛应用。其中最流行的STOMP消息代理是Apache ActiveMQ。
STOMP协议工作于TCP协议之上,使用了下列命令:
- SEND 发送
- SUBSCRIBE 订阅
- UNSUBSCRIBE 退订
- BEGIN 开始
- COMMIT 提交
- ABORT 取消
- ACK 确认
- DISCONNECT 断开
发送消息:
SEND
destination:/app/sendTest
content-type:application/json
content-length:44
{
"userId":"rwerfef45434refgrege"}
订阅消息:
SUBSCRIBE
id:sub-1
destination:/app/subscribeTest
服务器进行广播:
MESSAGE
message-id:nxahklf6-1
subscription:sub-1
destination:/topic/subscribeTest
{
"message":"it is test"}
更多详细的STOMP API,请点击这里
springboot使用STOMP消息步骤:
- 添加pom文件依赖
- java方式配置websocket stomp
- 消息实体类
- 书写控制层
- 书写客户端
项目目录结构:
1.添加pom文件依赖
<!-- springboot websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2.java方式配置websocket stomp
package com.ahut.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
/**
*
* @ClassName: WebSocketStompConfig
* @Description: springboot websocket stomp配置
* @author cheng
* @date 2017年9月27日 下午3:45:36
*/
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketStompConfig extends AbstractWebSocketMessageBrokerConfigurer {
/**
* 注册stomp的端点
*/
@Override
public void