Description
- Point-To-Point (ActiveMQ P2P Messaging)
- A message can be delivered to one receiver only. Here, Queue is used as a message-oriented middleware.
- The messages stay in the queue until received and processed.
- There is no timing-related dependency between the sender and receiver. Sent messages could be retrieved without any expiry.
Requirements
- JVM 17+
- ActiveMQ on Docker
Install ActiveMQ on Docker
docker-compose:
version: '3'
services:
activemq:
image: rmohr/activemq
ports:
- "61616:61616"
- "8161:8161"
volumes:
- data:/data
- conf:/conf
volumes:
data:
conf:Open the URL http://localhost:8161/admin
username: admin
password: admin

Project Structure

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jreact</groupId>
<artifactId>activemq-p2p</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>activemq-p2p</name>
<description>activemq-p2p test project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.0-alpha6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.0-alpha6</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<finalName>activemq</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>Testing Producer
Run ProducerMain.java

Testing Consumer
Run ConsumerMain.java

AUTO_ACKNOWLEDGE
– in SimpleQueue.java
Automatically sends a message acknowledgment back to the ActiveMQ broker for every message consumed.
Acknowledgment modes
| Acknowledgment mode | Sends an acknowledgment | Description |
|---|---|---|
| Session.SESSION_TRANSACTED | Rolls up acknowledgments with Session.commit(). | Reliable way for message consumption and performs well, providing you consume more than one message in a commit. |
| Session.CLIENT_ACKNOWLEDGE | All messages up to when a message is acknowledged are consumed. | Can perform well, providing the application consumes a lot of messages before calling acknowledge. |
| Session.AUTO_ACKNOWLEDGE | Automatically sends a message acknowledgment back to the ActiveMQ broker for every message consumed. | This can be slow but is often the default mechanism for message consumers. |
| Session.DUPS_OK_ACKNOWLEDGE | Allows the consumer to send one acknowledgment back to the ActiveMQ broker for a range of messages consumed. | An acknowledgment will be sent back when the prefetch limit has reached 50% full. The fastest standard way of consuming messages. |
| ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE | Sends one acknowledgment for every message consumed. | Allows great control by enabling messages to be acknowledged individually but can be slow. |
| optimizeAcknowledge | Allows the consumer to send one acknowledgment back to the ActiveMQ broker for a range of messages consumed. | A hint that works in conjunction with Session.AUTO_ACKNOWLEDGE. An acknowledgment will be sent back when 65% of the prefetch buffer has been consumed. This is the fastest way to consume messages. |
–
