1 简介

Solace是一个强大的实时性的事件驱动消息队列。本文将介绍如何在Spring中使用,虽然代码使用的是Spring Boot,但并没有使用相关starter,跟Spring的整合一样,可通用。JMS是通过的消息处理框架,可以深入学习一下,不同的MQ在JMS的整合上都是类似的。

2 通过Docker启动Solace

有两种方式试用Solace,一种是通过Docker来启动,另一种是使用Cloud版本,但Cloud版本有试用期限,我们使用Docker来启动吧。

先下载镜像:

$ docker pull solace/solace-pubsub-standard:9.13.0.16

然后通过以下命令启动:

$ docker run -d -p 8080:8080 -p 55554:55555 -p 8008:8008 -p 1883:1883 -p 8000:8000 -p 5672:5672 -p 9000:9000 -p 2222:2222 --shm-size=2g --env username_admin_globalaccesslevel=admin --env username_admin_password=admin --name=solace solace/solace-pubsub-standard:9.13.0.16

这里端口改为55554,是因为Mac的原因。

然后便可以访问来登陆管理界面:http://localhost:8080/

用户名密码为:admin/admin

登陆后可以看到如下界面,Solace按VPN来管理队列,VPN有点像分组,比如某个业务线使用某个VPN。

我们在default的VPN上创建一个Queue,名为pkslow-queue

其它设置如下:

接着在该Queue上创建Topic:

创建完成后,我们可以直接测试一下:

可以Publish到Topic或Queue,也可以从其中一个Subscribe。

完成以上设置后,我们就可以在Spring Boot中整合了。

3 Spring Boot JMS整合Solace

3.1 发送消息

我们是通过JmsTemplate来发送消息的,而JmsTemplate需要连接到MQ,就需要一个ConnectionFactory,这个Factory是带着MQ的一些连接信息。配置代码如下:

@Configuration
public class SolacePubConfig { private final SolaceProperties solaceProperties; public SolacePubConfig(SolaceProperties solaceProperties) {
this.solaceProperties = solaceProperties;
} @Bean("connectionFactory")
public ConnectionFactory connectionFactory() throws Exception {
Properties env = new Properties();
env.put(InitialContext.INITIAL_CONTEXT_FACTORY, "com.solacesystems.jndi.SolJNDIInitialContextFactory");
env.put(InitialContext.PROVIDER_URL, solaceProperties.getBrokerUrl());
env.put(SupportedProperty.SOLACE_JMS_VPN, solaceProperties.getVpn());
env.put(InitialContext.SECURITY_PRINCIPAL, solaceProperties.getUsername());
env.put(InitialContext.SECURITY_CREDENTIALS, solaceProperties.getPassword());
return SolJmsUtility.createConnectionFactory(env);
} @Bean
public CachingConnectionFactory cachingConnectionFactory(ConnectionFactory connectionFactory) {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory);
cachingConnectionFactory.setSessionCacheSize(10);
return cachingConnectionFactory;
} @Bean
public JmsTemplate pubJmsTemplate(CachingConnectionFactory cachingConnectionFactory) {
JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
jmsTemplate.setPubSubDomain(true);
jmsTemplate.setExplicitQosEnabled(true);
jmsTemplate.setDeliveryPersistent(true);
jmsTemplate.setDefaultDestinationName(solaceProperties.getDefaultPubDestinationName());
return jmsTemplate;
} }

生成JmsTemplate后,就可以引用并发送消息了:

@RestController
@RequestMapping("/solace")
public class SolaceTestController {
private final JmsTemplate pubJmsTemplate;
private final SolaceProperties solaceProperties; public SolaceTestController(JmsTemplate pubJmsTemplate, SolaceProperties solaceProperties) {
this.pubJmsTemplate = pubJmsTemplate;
this.solaceProperties = solaceProperties;
} @GetMapping
public String send() {
pubJmsTemplate.send(solaceProperties.getDefaultPubDestinationName(), session -> session.createTextMessage("www.pkslow.com"));
pubJmsTemplate.send(session -> session.createTextMessage("Larry Deng"));
return "OK";
}
}

用到的属性配置如下:

server.port=8083

pkslow.solace.brokerUrl=smf://127.0.0.1:55554
pkslow.solace.vpn=default
pkslow.solace.username=default
pkslow.solace.password=default
pkslow.solace.defaultPubDestinationName=pkslow-topic
pkslow.solace.defaultSubDestinationName=pkslow-queue
@Configuration
@ConfigurationProperties(prefix = "pkslow.solace")
@Setter
@Getter
public class SolaceProperties {
private String brokerUrl;
private String vpn;
private String username;
private String password;
private String defaultPubDestinationName;
private String defaultSubDestinationName;
}

3.2 接收消息

我们通过MessageListenerContainer来接收消息,MessageListenerContainer也需要一个ConnectionFactory,也有MQ的连接信息。还需要一个MessageListener,用来定义如何处理消息。我们的配置如下:

@Configuration
@Slf4j
public class SolaceSubConfig {
private final SolaceProperties solaceProperties; public SolaceSubConfig(SolaceProperties solaceProperties) {
this.solaceProperties = solaceProperties;
} @Bean
public SingleConnectionFactory singleConnectionFactory(@Qualifier("connectionFactory") ConnectionFactory targetConnectionFactory) {
return new SingleConnectionFactory(targetConnectionFactory);
} @Bean
public MessageListener messageListener() {
return message -> {
try {
log.info("Received message " + ((TextMessage) message).getText() + " on destination: " +
message.getJMSDestination().toString());
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
};
} @Bean
public MessageListenerContainer messageListenerContainer(SingleConnectionFactory singleConnectionFactory, MessageListener messageListener) {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setConnectionFactory(singleConnectionFactory);
container.setDestinationName(solaceProperties.getDefaultSubDestinationName());
container.setMessageListener(messageListener); return container;
}
}

这里@Qualifier("connectionFactory") ConnectionFactory targetConnectionFactory复用了在SolacePubConfig创建的对象。

3.3 测试

发送GET请求就可以触发发送了:

GET http://localhost:8083/solace

我发了三次,结果日志如下:

4 代码

代码请看GitHub: https://github.com/LarryDpk/pkslow-samples


References:

Docker available image tags

Docker Solace Guide

Spring Solace

通过Docker启动Solace,并在Spring Boot通过JMS整合Solace的更多相关文章

  1. 右击main 方法运行正常,启动tomcat 后,spring boot 项目 出现参数字符串是乱码的情况

    PrintWriter out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8")) ...

  2. Spring Boot入门 and Spring Boot与ActiveMQ整合

    1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...

  3. Spring Boot和Dubbo整合

    provider端 POM依赖 <dependencies> <dependency> <groupId>org.springframework.boot</ ...

  4. RabbitMQ入门:在Spring Boot 应用中整合RabbitMQ

    在上一篇随笔中我们认识并安装了RabbitMQ,接下来我们来看下怎么在Spring Boot 应用中整合RabbitMQ. 先给出最终目录结构: 搭建步骤如下: 新建maven工程amqp 修改pom ...

  5. Spring Boot与ActiveMQ整合

    Spring Boot与ActiveMQ整合 1使用内嵌服务 (1)在pom.xml中引入ActiveMQ起步依赖 <dependency> <groupId>org.spri ...

  6. Spring Boot 2.X整合Spring-cache,让你的网站速度飞起来

    计算机领域有人说过一句名言:“计算机科学领域的任何问题都可以通过增加一个中间层来解决”,今天我们就用Spring-cache给网站添加一层缓存,让你的网站速度飞起来. 本文目录 一.Spring Ca ...

  7. Spring Boot 2.0 整合携程Apollo配置中心

    原文:https://www.jianshu.com/p/23d695af7e80 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够 ...

  8. Spring Boot 2.x整合Redis

    最近在学习Spring Boot 2.x整合Redis,在这里和大家分享一下,希望对大家有帮助. Redis是什么 Redis 是开源免费高性能的key-value数据库.有以下的优势(源于Redis ...

  9. spring boot 2.0 整合 elasticsearch6.5.3,spring boot 2.0 整合 elasticsearch NoNodeAvailableException

    原文地址:spring boot 2.0 整合 elasticsearch NoNodeAvailableException 原文说的有点问题,下面贴出我的配置: 原码云项目地址:https://gi ...

  10. 转-Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合

    Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合 http://blog.csdn.net/balabalayi/article/detai ...

随机推荐

  1. Python学习三天计划-3

    面向对象 一.类的定义 1.类定义 class是关键字,表示要定义类了 类的属性,即定义在类中的变量(成员变量) 类的行为,即定义在类中的函数(成员方法) 2.对象 创建类对象的语法: class S ...

  2. 「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景

    大家好,我是melo,一名大三后台练习生 专栏回顾 索引的原理&&设计原则 欢迎关注本专栏:MySQL高级篇 本篇速览 在我们上一篇文章中,讲到了索引的原理&&设计原则 ...

  3. Linux操作系统,笔录!

    1.Linux 1.1.Linux介绍: Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX ...

  4. 基于Spring的发布订阅模式 EventListener

    基于Spring的发布订阅模式 在我们使用spring开发应用时,经常会碰到要去解耦合一些依赖调用,比如我们在做代码的发布流程中,需要去通知相关的测试,开发人员关注发布中的错误信息.而且通知这个操作又 ...

  5. Springboot实现验证码登录

    Springboot实现验证码登录 1.背景 本人近期正在完成毕业设计(旅游信息管理系统)的制作,采用的SpringBoot+Thymeleaf的模式.在登录网站时想要添加验证码验证,通过网上查找资料 ...

  6. [CS61A] Lecture 1&2&3. Introduction&Functions&Control

    [CS61A] Lecture 1&2&3. Introduction&Functions&Control 前言 CS61A是加州大学伯克利分校一门计算机专业课程,用于 ...

  7. 【Docker】容器使用规范--安全挂载建议

    容器挂载过程和安全挂载建议 绑定挂载 本文所提到的挂载主要指绑定挂载(bind mount),即通过-v /xx/xx:/xx/xx 和 --mount type=bind,xxx,xxx两种方式设置 ...

  8. JUC学习笔记——并发工具线程池

    JUC学习笔记--并发工具线程池 在本系列内容中我们会对JUC做一个系统的学习,本片将会介绍JUC的并发工具线程池 我们会分为以下几部分进行介绍: 线程池介绍 自定义线程池 模式之Worker Thr ...

  9. 解决mysql本地连接速度慢

    解决方法 用127.0.0.1而不用localhost 原因 听说是有什么DNS的反向解析

  10. linux内核源码下载地址

    一.官网链接 https://www.kernel.org/ 二.HTTP https://www.kernel.org/pub/ 三.GIT https://git.kernel.org/ 四.镜像 ...