https://github.com/libgraviton/messaging-java
Message Queue Integration Library for Java
https://github.com/libgraviton/messaging-java
java jms messaging rabbitmq
Last synced: about 1 year ago
JSON representation
Message Queue Integration Library for Java
- Host: GitHub
- URL: https://github.com/libgraviton/messaging-java
- Owner: libgraviton
- License: gpl-3.0
- Archived: true
- Created: 2017-02-10T07:59:20.000Z (over 9 years ago)
- Default Branch: develop
- Last Pushed: 2020-03-23T08:12:26.000Z (about 6 years ago)
- Last Synced: 2025-02-28T19:28:29.013Z (over 1 year ago)
- Topics: java, jms, messaging, rabbitmq
- Language: Java
- Size: 115 KB
- Stars: 1
- Watchers: 14
- Forks: 1
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# messaging-java
[](https://travis-ci.org/libgraviton/messaging-java) [](https://coveralls.io/github/libgraviton/messaging-java?branch=develop) [](https://maven-badges.herokuapp.com/maven-central/com.github.libgraviton/messaging) [](https://javadocio-badges.herokuapp.com/com.github.libgraviton/messaging)
Message Queue Integration Library for Java
## Supported Message Brokers
* RabbitMQ
* JMS based Message Brokers
## Using the library
### Setup
In order to use the library, include the following in the `pom.xml` of your project:
```xml
com.github.libgraviton
messaging
LATEST
```
Make sure that `version` points to the newest release on maven central (see badge above).
### Publish Messages
To publish messages you need an instance of `QueueConnection`, which represents the connection to the Message Queue.
Once you have a `QueueConnection`, you can simply do the following to publish a message:
```java
QueueConnection connection = new RabbitMqConnection.Builder().queueName("your-queue").build();
try {
connection.publish("the message");
} catch (CannotPublishMessage e) {
// Message publishment failed for some reason.
fail(String.format("An exception occurred: '%s'", e.getClass().getName()));
}
```
### Consume Messages
To consume messages you need an instance of `QueueConnection`, which represents the connection to the Message Queue.
Once you have a `QueueConnection`, you can simply do the following to consume a message:
```java
Consumer consumer = new Consumer() {
@Override
public void consume(String messageId, String message) throws CannotConsumeMessage {
System.out.println(String.format("Received message with id '%s': '%s'", messageId, message));
}
};
QueueConnection connection = new RabbitMqConnection.Builder().queueName("your-queue").build();
try {
connection.consume(consumer);
} catch (CannotRegisterConsumer e) {
// Consumer registration failed for some reason.
fail(String.format("An exception occurred: '%s'", e.getClass().getName()));
}
```
In this case, each message gets automatically acknowledged. If you want to handle message acknowledgment yourself, you need to register an `AcknowledgingConsumer`:
```java
Consumer consumer = new AcknowledgingConsumer() {
private MessageAcknowledger acknowledger;
@Override
public void setAcknowledger(MessageAcknowledger acknowledger) {
this.acknowledger = acknowledger;
}
@Override
public void consume(String messageId, String message) throws CannotConsumeMessage {
System.out.println(String.format("Received message with id '%s': '%s'", messageId, message));
try {
acknowledger.acknowledge(messageId);
} catch (CannotAcknowledgeMessage e) {
// Message Acknowledgment failed for some reason
throw new CannotConsumeMessage(messageId, message, e);
}
}
};
QueueConnection connection = new RabbitMqConnection.Builder().queueName("your-queue").build();
try {
connection.consume(consumer);
} catch (CannotRegisterConsumer e) {
// Consumer registration failed for some reason.
fail(String.format("An exception occurred: '%s'", e.getClass().getName()));
}
```