day4 java消息中间件服务
关于ActiveMQ、RocketMQ、RabbitMQ、Kafka一些总结和区别
ActiveMQ之消息服务器平台(发邮件)!!!!

PS: 讲个故事,老王要给他的两个女儿讲故事,他要一个一个讲很费劲,后来他使用了微信公众号,让订阅微信公众号的人关注就减轻了负担。


PS: 传统的如果一个用户进行登录,会调用分多的服务,如果没有消息中间件等待的时间就会很长(这样同步的效率很低),有了消息中间件首先
能有异步的保证登录,然后还能保证服务不会被一个一个执行

PS:生活中的应用













PS: kafka性能高,但是数据会丢失
rabbitmp保证数据不丢失,性能比activemq强
activeMq满足80%以上的业务场景
关于消息队列的介绍----ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ
PS: JMS是一种规范,不同的公司有不同的实现,其中最常用的是ActiveMQ, 类似于JDBC感觉
队列是一两万,消息流量几十万条也是可以使用的

PS: 也就是对方不在的时候也可以发送


PS: JMS规定点对点 和 发布和订阅模式, 如果是点对点的模式就把数据扔到 Queue中,如果是 发布订阅就发送到topic中;
消息中间件可以理解为 数据库,通过ConnecttionFactory连接,从而获得一个Connecttion,然后把消息发送到指定的目的地

--------------------------------------------------------------------------------------------










PS:这是Java Message Service的体系。
PS:体系架构是重点,分为消费者和发送者两者。统称为客户
PS: 消息的两种模式 : 队列和话题

1.下载ActiveMQ
去官方网站下载:http://activemq.apache.org/
2.运行ActiveMQ
解压缩apache-activemq-5.5.1-bin.zip,
PS :要用这个老版本的; 后来学习的时候使用5.11.1

修改配置文件activeMQ.xml,将0.0.0.0修改为localhost
|
<transportConnectors> <transportConnector name="openwire" uri="tcp://localhost:61616"/> <transportConnector name="ssl" uri="ssl://localhost:61617"/> <transportConnector name="stomp" uri="stomp://localhost:61613"/> <transportConnector uri="http://localhost:8081"/> <transportConnector uri="udp://localhost:61618"/> |

然后双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。



启动ActiveMQ以后,登陆:http://localhost:8161/admin/,创建一个Queue,命名为FirstQueue。

PS:不会的话,直接看百度怎么配置JMS
1.安装包2.修改配置 3.bat启动 4.执行代码看业务流程 PS :安全机制主要是配置用户



PS: 配置好的话,会自动创建三个表,处理完数据以后自动会清空


PS: 事物的设置主要在发送端, 最后一种提交方式用的比较少

------------------------------------------------------------------------------------------------------------------------------------------------------

PS:JMS用在J2EE生产环境中,kafka用在大数据开发环境中 。 都是消息队列的处理。。。。
PS:比如上图,在web系统接收数据,然后会生成相应的日志文件,应用kafka读取数据,然后再执行相应的业务处理。

package cn.itcast_03_mq.topic;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; public class ProducerTool {
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private String subject = "mytopic";
private Destination destination = null;
private Connection connection = null;
private Session session = null;
private MessageProducer producer = null;
// 初始化
private void initialize() throws JMSException, Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url);
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createTopic(subject);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
// 发送消息
public void produceMessage(String message) throws JMSException, Exception {
initialize();
TextMessage msg = session.createTextMessage(message);
connection.start();
System.out.println("Producer:->Sending message: " + message);
producer.send(msg);
System.out.println("Producer:->Message sent complete!");
}
// 关闭连接
public void close() throws JMSException {
System.out.println("Producer:->Closing connection");
if (producer != null)
producer.close();
if (session != null)
session.close();
if (connection != null)
connection.close();
}
}
package cn.itcast_03_mq.topic;
import java.util.Random; import javax.jms.JMSException; public class ProducerTest { /**
* @param args
*/
public static void main(String[] args) throws JMSException, Exception {
ProducerTool producer = new ProducerTool();
Random random = new Random();
for(int i=0;i<20;i++){ Thread.sleep(random.nextInt(10)*1000); producer.produceMessage("Hello, world!--"+i);
producer.close();
} }
}
package cn.itcast_03_mq.topic;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.MessageListener;
import javax.jms.Message;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; public class ConsumerTool implements MessageListener,ExceptionListener {
//这些都是体系架构的东西
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url =ActiveMQConnection.DEFAULT_BROKER_URL;
private String subject = "mytopic";
private Destination destination = null;
private Connection connection = null;
private Session session = null;
private MessageConsumer consumer = null;
public static Boolean isconnection=false;
// 初始化
private void initialize() throws JMSException, Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url); //创建工厂
connection = connectionFactory.createConnection(); //创建链接
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createTopic(subject);
consumer = session.createConsumer(destination);
} // 消费消息
public void consumeMessage() throws JMSException, Exception {
initialize();
connection.start();
consumer.setMessageListener(this); //监听消息
connection.setExceptionListener(this);//异常监听
isconnection=true;
System.out.println("Consumer:->Begin listening...");
// 开始监听
// Message message = consumer.receive();
}
// 关闭连接
public void close() throws JMSException {
System.out.println("Consumer:->Closing connection");
if (consumer != null)
consumer.close();
if (session != null)
session.close();
if (connection != null)
connection.close();
}
// 消息处理函数
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String msg = txtMsg.getText();
System.out.println("Consumer:->Received: " + msg);
} else {
System.out.println("Consumer:->Received: " + message);
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void onException(JMSException arg0) {
isconnection=false;
}
}
package cn.itcast_03_mq.topic;
import javax.jms.JMSException;
public class ConsumerTest implements Runnable {
static Thread t1 = null;
/**
* @param args
* @throws InterruptedException
* @throws InterruptedException
* @throws JMSException
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
t1 = new Thread(new ConsumerTest());
t1.setDaemon(false);
t1.start();
/**
* 如果发生异常,则重启consumer
*/
/*while (true) {
System.out.println(t1.isAlive());
if (!t1.isAlive()) {
t1 = new Thread(new ConsumerTest());
t1.start();
System.out.println("重新启动");
}
Thread.sleep(5000);
}*/
// 延时500毫秒之后停止接受消息
// Thread.sleep(500);
// consumer.close();
}
public void run() {
try {
ConsumerTool consumer = new ConsumerTool();
consumer.consumeMessage();
while (ConsumerTool.isconnection) {
}
} catch (Exception e) {
}
}
}

PS:此为程序启动以后监听的效果
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/All_Downloads/BaiduYunDownload/chuanzhiboke%e5%a4%a7%e6%95%b0%e6%8d%ae/%e8%a7%86%e9%a2%91/day04%e5%b9%b6%e5%8f%91/day04/%e4%bb%a3%e7%a0%81/1-cloudDay04/lib/activemq-all-5.9.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/All_Downloads/BaiduYunDownload/chuanzhiboke%e5%a4%a7%e6%95%b0%e6%8d%ae/%e8%a7%86%e9%a2%91/day04%e5%b9%b6%e5%8f%91/day04/%e4%bb%a3%e7%a0%81/1-cloudDay04/lib/slf4j-simple-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2017-11-16 19:12:57,790 [ActiveMQ Task-1] INFO org.apache.activemq.transport.failover.FailoverTransport.doReconnect(FailoverTransport.java:1040) method:doReconnect Successfully connected to tcp://localhost:61616
Consumer:->Begin listening...
Consumer:->Received: Hello, world!--0
Consumer:->Received: Hello, world!--1
Consumer:->Received: Hello, world!--2
Consumer:->Received: Hello, world!--3
Consumer:->Received: Hello, world!--4
Consumer:->Received: Hello, world!--5
Consumer:->Received: Hello, world!--6
Consumer:->Received: Hello, world!--7
Consumer:->Received: Hello, world!--8
Consumer:->Received: Hello, world!--9
Consumer:->Received: Hello, world!--10
Consumer:->Received: Hello, world!--11
Consumer:->Received: Hello, world!--12
Consumer:->Received: Hello, world!--13
Consumer:->Received: Hello, world!--14
Consumer:->Received: Hello, world!--15
Consumer:->Received: Hello, world!--16
Consumer:->Received: Hello, world!--17
Consumer:->Received: Hello, world!--18
Consumer:->Received: Hello, world!--19
PS :总结
PS: activemp只是用在javaee中,在现实生产中目前都用kafka了

PS:JMS用在J2EE生产环境中,kafka用在大数据开发环境中 。 都是消息队列的处理。。。。
PS:比如上图,在web系统接收数据,然后会生成相应的日志文件,应用kafka读取数据,然后再执行相应的业务处理。
下面介绍spring Jms理论



步骤
1.配置spring application.xml





PS :动态可以拓展



day4 java消息中间件服务的更多相关文章
- JMS(Java消息服务)与消息队列ActiveMQ基本使用(一)
最近的项目中用到了mq,之前自己一直在码农一样的照葫芦画瓢.最近几天研究了下,把自己所有看下来的文档和了解总结一下. 一. 认识JMS 1.概述 对于JMS,百度百科,是这样介绍的:JMS即Java消 ...
- java消息服务学习之JMS概念
JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信. ...
- Java消息中间件入门笔记 - ActiveMQ篇
入门 消息中间件带来的好处: 1)解耦:系统解耦 2)异步:异步执行 3)横向扩展 4)安全可靠 5)顺序保证 栗子: 通过服务调用让其它系统感知事件发生 系统之间高耦合 程序执行效率低 通过消息中间 ...
- 以ActiveMQ为例JAVA消息中间件学习【1】
前言 在慢慢的接触大型的javaweb的项目就会接触到很多的中间件系统. 其中消息中间件在很多场景下会被运用. 这里主要就对最近所学习到的消息中间件知识做一个笔记,为以后的实际运用打下一个良好的基础. ...
- 【转载】JAVA消息服务JMS规范及原理详解
转载:https://www.cnblogs.com/molao-doing/articles/6557305.html 作者: moyun- 一.简介 JMS即Java消息服务(Java Messa ...
- JAVA消息服务JMS规范及原理详解
JAVA消息服务JMS规范及原理详解 一.简介 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应 ...
- JMS(Java消息服务)
JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM:指的是利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来 ...
- java消息中间件的使用与简介
一.为什么要使用消息中间件 消息中间件就是可以省去繁琐的步骤,直达目的,怎么讲呢,就是比如你想很多人,知道你的动态,而知道的人可能手机没电,可能手机信号不好,可能手机不在服务区,或者看的人比较忙,看的 ...
- java消息中间件入门
消息中间件来解耦服务调用 比如1个登录系统,登录的话需要调用很多系统的其他服务,如果中间调用失败,可能会导致登录信息一致无法返回,同时也增加了系统的耦合度.而用消息中间件的话,则是不发送服务到其他系统 ...
随机推荐
- getopts的使用方法
getopts的使用 语法格式:getopts [option[:]] [DESCPRITION] VARIABLE option:表示为某个脚本可以使用的选项 ":":如果某个选 ...
- Python中os与sys模块的区别
os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...
- Pamulinawen--IPA--菲律宾伊洛卡诺语
这是一首菲律宾的民谣(不是他加禄语/Tagalog, 而是伊洛卡诺语/Ilokano), 我们国家的著名歌手朱明瑛也翻唱过, 歌曲中文名为<<田野之歌>>.
- intelij idea常用设置
1.genneral设置 2.自动导包 3.设置显示行号和方法分隔符 4.忽略大小写提示代码 比如:输入str会让其提示String 5.去掉单行显示类,让idea多行显示,容易找到类 6.设置字体及 ...
- idea查看jar包是否存在
idea在project目录下如下图(1),是总的pom文件,定义了四个子模块共用的依赖,并且其中定义了四个子模块,,每个模块都有各自的pom.xml文件.结构目录只有一个总的lib库. 但是可能在s ...
- 牛客多校第四场 F Beautiful Garden
链接:https://www.nowcoder.com/acm/contest/142/F来源:牛客网 题目描述 There's a beautiful garden whose size is n ...
- Array.apply(null, {length: 20})和Array(20)的理解
话说今晚在学习Vue.js教程里:Render函数,这一章节是发现了一个问题,就是利用下面的这个render函数可以渲染20个重复的段落: render: function (createElemen ...
- position:sticky粘性布局
新的布局方式,专门用于 tab栏悬浮效果: 当tab栏在可视区域时,正常滚动, tab栏不再可视区域时,悬浮置顶. position:-webkit-sticky; position:sticky; ...
- Delphi 10.3.1来了
10.3.1发布了,这个版本可以独自安装,是对Delphi 10.3 Rio,C ++ Builder 10.3 Rio和RAD Studio 10.3 Rio的更新.如果安装了2018年11月发布的 ...
- vuejs中v-bind绑定class时的注意事项
关于v-bind绑定class的实例 作用:可用于不同样式之间的切换 <!DOCTYPE html> <html lang="en"> <head&g ...



