Table of Contents
1. Message storage method" >1. Message storage method
三、AMQ消息存储" >三、AMQ消息存储
四、JDBC存储" >四、JDBC存储
五、内存消息存储" >五、内存消息存储
Home Java javaTutorial Detailed explanation of JMS Active MQ message storage instance

Detailed explanation of JMS Active MQ message storage instance

Jun 26, 2017 am 11:39 AM
active storage information

1. Message storage method

ActiveMQ supports persistent messages and non-persistent messages in the JMS specification

  • Persistent messages Usually used to ensure that messages will be consumed by the consumer regardless of whether the consumer is online or not. When the message is confirmed to be consumed, it will be deleted from the storage

  • Non-persistent messages are usually used to send notifications and real-time data. Performance is usually required first, and message reliability is not a must.

  • MQ supports pluggable message storage, such as memory, file and relational database

Queue message model Storage in ActiveMQ

adopts first-in-first-out (FIFO) storage. A message can only be consumed by one consumer, and will be deleted only after the message is confirmed to be consumed.

Topic message model (for durable subscription)
The message obtained by each subscriber is actually a copy of the message. Only one copy of the message will be stored. MQ provides a pointer To point to the message store and distribute a copy of the message to subscribers, the message cannot be deleted until all persistent subscribers have received it.

Persistent storage method:

  1. KahaDB message storage

  2. AMQ message storage

  3. JDBC message storage

  4. Memory message storage

2. KahaDB storage method

KahaDB is the default persistence plug-in starting from ActiveMQ 5.4. KahaDb's recovery time is much shorter than its predecessor AMQ and uses fewer data files, so it can completely replace AMQ. KahaDB's persistence mechanism is also based on log files, indexes and caches.

(1) Main features of KahaDB:

  1. Store messages in log form;

  2. The message index is stored in a B-Tree structure and can be updated quickly;

  3. Fully supports JMS transactions;

  4. Supports a variety of Recovery mechanism;

(2) Applicable scenarios:

  1. High throughput Application

  2. Storing messages with large amounts of data

(3) Configuration method conf/activemq.xml:

       <persistenceAdapter><kahaDB directory="${activemq.data}/kahadb"/></persistenceAdapter>
Copy after login

(四)、KahaDB存储原理:

    当有活动消费者时,用于临时存储,消息会被发送给消费着,同时被安排将被存储,如果消息及时被确认,就不需要写入到磁盘。写入到磁盘中的数据消息,在后续的消息活动中,如果消息发送成功,变标记为可删除的。系统会周期性的清除或者归档日志文件。

  1、KahaDB内部结构

Data logs:消息日志包含了消息日志和一些命令
Cache:当有活动消费者时,用于临时存储,消息会被发送给消费着,同时被安排将被存储,如果消息及时被确认,这不需要写入到磁盘
Btree indexes(消息索引):用于引用消息日志(message id),它存储在内存中,这样能快速定位到。MQ会定期将内存中的消息索引保存到metadata store中,避免大量消息未发送时,消息索引占用过多内存空间。
Redo log用于在非正常关机情况下维护索引完整性。

2、目录结构:

 

Db log files:用于存储消息(默认大小32M),当log日志满了,会创建一个新的,当log日志中的消息都被删除,该日志文件会被删除或者归档。
Archive directory:当datalog不在被kahadb需要会被归档(通过archiveDataLogs属性控制)。
Db.data:存放Btree indexs。
Db.redo:存放redo file,用于恢复Btree indexs。

三、AMQ消息存储

  写入消息时,会将消息写入日志文件,由于是顺序追加写,性能很高。为了提升性能,创建消息主键索引,并且提供缓存机制,进一步提升性能。每个日志文件的大小都是有限制的(默认32m,可自行配置)。当超过这个大小,系统会重新建立一个文件。当所有的消息都消费完成,系统会删除这个文件或者归档(取决于配置)。主要的缺点是AMQ Message会为每一个Destination创建一个索引,如果使用了大量的Queue,索引文件的大小会占用很多磁盘空间。而且由于索引巨大,一旦Broker崩溃,重建索引的速度会非常慢。

特点:类似KahaDB,也包含了事务日志,每个destination都包含一个index文件,AMQ适用于高吞吐量的应用场景,但是不适合多个队列的场景。

 配置方式conf/activemq.xml:

       <!--AMQ    directory:数据存储路径 syncOnWrite:是否同步写入  maxFileLength:日志文件大小 --><persistenceAdapter><amqPersistenceAdapterdirectory="${activemq.data}/AMQdb"syncOnWrite="true"maxFileLength="10mb" /></persistenceAdapter>
Copy after login

1、AMQ内部结构:

 

Data logs:消息日志包含了消息日志
Cache:用于消息的快速检索
Reference store indexes:用于引用datalogs中的消息,通过message ID 关联

2、目录结构:

Lock:保证同一时间只有一个borker访问文件目录
temp-storag:用于存储非持久化消息(当不在被存储在内存中),如等待慢消费者处理消息
Kr-store:用于存储引用消息日志数据
journal directory:包含了消息文件、消息日志和消息控制信息
Archive:归档的数据日志

四、JDBC存储

支持通过JDBC将消息存储到关系数据库,性能上不如文件存储,能通过关系型数据库查询到消息的信息。

MQ支持的数据库:Apache Derby、MYsql、PostgreSQL、Oracle、SQLServer、Sybase、Informix、MaxDB。

存储表结构:

A、ACTIVEMQ_MSGS:用于存储消息,Queue和Topic都存储在这个表中:

  • ID:自增的数据库主键

  • CONTAINER:消息的Destination

  • MSGID_PROD:消息发送者客户端的主键

  • MSG_SEQ:是发送消息的顺序,MSGID_PROD+MSG_SEQ可以组成JMS的MessageID

  • EXPIRATION:消息的过期时间,存储的是从1970-01-01到现在的毫秒数

  • MSG:消息本体的Java序列化对象的二进制数据

  • PRIORITY:优先级,从0-9,数值越大优先级越高

B、ACTIVEMQ_ACKS:用于存储订阅关系。如果是持久化Topic,订阅者和服务器的订阅关系在这个表保存:

  • 主要的数据库字段如下:

  • CONTAINER:消息的Destination

  • SUB_DEST:如果是使用Static集群,这个字段会有集群其他系统的信息

  • CLIENT_ID:每个订阅者都必须有一个唯一的客户端ID用以区分

  • SUB_NAME:订阅者名称

  • SELECTOR:选择器,可以选择只消费满足条件的消息。条件可以用自定义属性实现,可支持多属性AND和OR操作

  • LAST_ACKED_ID:记录消费过的消息的ID。

C、ACTIVEMQ_LOCK(消息锁,保证同一时间只能有一个broker访问这些表结构):
        表activemq_lock在集群环境中才有用,只有一个Broker可以获得消息,称为Master Broker,其他的只能作为备份等待Master Broker不可用,才可能成为下一个Master Broker。这个表用于记录哪个Broker是当前的Master Broker。

 

配置方式:

1、配置数据源 conf/acticvemq.xml文件:

 <!-- 配置数据源-->  <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/activemq?relaxAutoCommit=true"/><property name="username" value="root"/><property name="password" value="111111"/><property name="maxActive" value="200"/><property name="poolPreparedStatements" value="true"/>  </bean>
Copy after login

2、配置broke中的persistenceAdapter :

dataSource指定持久化数据库的bean,createTablesOnStartup是否在启动的时候创建数据表,默认值是true,这样每次启动都会去创建数据表了,一般是第一次启动的时候设置为true,之后改成false。

 <!-- JDBC配置 --><persistenceAdapter><jdbcPersistenceAdapter dataSource="#mysql-ds"  createTablesOnStartup="false"/></persistenceAdapter>
Copy after login

ps:数据库activemq 需要手动创建。

五、内存消息存储

内存消息存储,会将所有的持久化消息存储在内存中,必须注意JVM使用情况以及内存限制,适用于一些能快速消费的数据量不大的小消息,当MQ关闭或者宕机,未被消费的内存消息会被清空。

配置方式 设置 broker属性值  persistent="false":

    <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" persistent="false">
Copy after login

 

The above is the detailed content of Detailed explanation of JMS Active MQ message storage instance. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to swipe right and reply quickly in iMessage on iOS 17 How to swipe right and reply quickly in iMessage on iOS 17 Sep 20, 2023 am 10:45 AM

How to Use Swipe to Reply in iMessages on iPhone Note: The Swipe to Reply feature only works with iMessage conversations in iOS 17, not regular SMS conversations in the Messages app. Open the Messages app on your iPhone. Then, head to the iMessage conversation and simply swipe right on the iMessage you want to reply to. Once this is done, the selected iMessage will be in focus while all other messages will be blurred in the background. You'll see a text box for typing a reply and a "+" icon for accessing iMessage apps like Check-ins, Places, Stickers, Photos, and more. Just enter your message,

What does it mean when a message has been sent but rejected by the other party? What does it mean when a message has been sent but rejected by the other party? Mar 07, 2024 pm 03:59 PM

The message has been sent but rejected by the other party. This means that the sent information has been successfully sent from the device, but for some reason, the other party did not receive the message. More specifically, this is usually because the other party has set certain permissions or taken certain actions, which prevents your information from being received normally.

Huawei will launch innovative MED storage products next year: rack capacity exceeds 10 PB and power consumption is less than 2 kW Huawei will launch innovative MED storage products next year: rack capacity exceeds 10 PB and power consumption is less than 2 kW Mar 07, 2024 pm 10:43 PM

This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

iOS 17: How to use emojis as stickers in Messages iOS 17: How to use emojis as stickers in Messages Sep 18, 2023 pm 05:13 PM

In iOS17, Apple has added several new features to its Messages app to make communicating with other Apple users more creative and fun. One of the features is the ability to use emojis as stickers. Stickers have been around in the Messages app for years, but so far, they haven't changed much. This is because in iOS17, Apple treats all standard emojis as stickers, allowing them to be used in the same way as actual stickers. This essentially means you're no longer limited to inserting them into conversations. Now you can also drag them anywhere on the message bubble. You can even stack them on top of each other to create little emoji scenes. The following steps show you how it works in iOS17

Vue3+TS+Vite development skills: how to encrypt and store data Vue3+TS+Vite development skills: how to encrypt and store data Sep 10, 2023 pm 04:51 PM

Vue3+TS+Vite development tips: How to encrypt and store data. With the rapid development of Internet technology, data security and privacy protection are becoming more and more important. In the Vue3+TS+Vite development environment, how to encrypt and store data is a problem that every developer needs to face. This article will introduce some common data encryption and storage techniques to help developers improve application security and user experience. 1. Data Encryption Front-end Data Encryption Front-end encryption is an important part of protecting data security. Commonly used

How to clear cache on Windows 11: Detailed tutorial with pictures How to clear cache on Windows 11: Detailed tutorial with pictures Apr 24, 2023 pm 09:37 PM

What is cache? A cache (pronounced ka·shay) is a specialized, high-speed hardware or software component used to store frequently requested data and instructions, which in turn can be used to load websites, applications, services, and other aspects of the system faster part. Caching makes the most frequently accessed data readily available. Cache files are not the same as cache memory. Cache files refer to frequently needed files such as PNGs, icons, logos, shaders, etc., which may be required by multiple programs. These files are stored in your physical drive space and are usually hidden. Cache memory, on the other hand, is a type of memory that is faster than main memory and/or RAM. It greatly reduces data access time since it is closer to the CPU and faster compared to RAM

The message has been sent but was rejected by the other party. Should I block it or delete it? The message has been sent but was rejected by the other party. Should I block it or delete it? Mar 12, 2024 pm 02:41 PM

1. Being added to the blacklist: The message has been sent but rejected by the other party. Generally, you have been blacklisted. At this time, you will not be able to send messages to the other party, and the other party will not be able to receive your messages. 2. Network problems: If the recipient's network condition is poor or there is a network failure, the message may not be successfully received. At this point, you can try to wait for the network to return to normal before sending the message again. 3. The other party has set up Do Not Disturb: If the recipient has set up Do Not Disturb in WeChat, the sender’s messages will not be reminded or displayed within a certain period of time.

Git installation process on Ubuntu Git installation process on Ubuntu Mar 20, 2024 pm 04:51 PM

Git is a fast, reliable, and adaptable distributed version control system. It is designed to support distributed, non-linear workflows, making it ideal for software development teams of all sizes. Each Git working directory is an independent repository with a complete history of all changes and the ability to track versions even without network access or a central server. GitHub is a Git repository hosted on the cloud that provides all the features of distributed revision control. GitHub is a Git repository hosted on the cloud. Unlike Git which is a CLI tool, GitHub has a web-based graphical user interface. It is used for version control, which involves collaborating with other developers and tracking changes to scripts and

See all articles