-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
213 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# DINA Messaging | ||
|
||
|
||
## Documentation | ||
|
||
RabbitMQ beans will be created if `isConsumer` or `isProducer` is set. | ||
|
||
Queues are handled by the different projects using that library. | ||
|
||
Messaging properties: | ||
|
||
``` | ||
dina.messaging.isConsumer="true" | ||
dina.messaging.isProducer="true" | ||
``` | ||
|
||
RabbitMQ properties: | ||
``` | ||
rabbitmq: | ||
host: localhost | ||
username: user | ||
password: password | ||
port: 15672 | ||
``` | ||
|
||
## Artifact | ||
|
||
`dina-messaging` artifact is published on Maven Central. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.github.aafc-bicoe</groupId> | ||
<artifactId>dina-base-parent</artifactId> | ||
<version>0.113</version> | ||
</parent> | ||
|
||
<artifactId>dina-messaging</artifactId> | ||
<name>dina-messaging</name> | ||
<description>Base DINA messaging classes based on RabbitMQ</description> | ||
<url>https://github.com/AAFC-BICoE/dina-base-api</url> | ||
|
||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-amqp</artifactId> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-logging</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<licenses> | ||
<license> | ||
<name>MIT License</name> | ||
<url>https://opensource.org/licenses/mit-license</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
</project> |
25 changes: 25 additions & 0 deletions
25
...aging/src/main/java/ca/gc/aafc/dina/messaging/config/MessagingConfigurationCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ca.gc.aafc.dina.messaging.config; | ||
|
||
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
|
||
/** | ||
* Condition that returns true if messaging consumer OR producer is set. | ||
* This class introduce a condition based on values coming from 2 different @ConditionalOnProperty | ||
* values. Values1 and value2 are evaluated (true/false) and combined with an OR operator. | ||
* | ||
*/ | ||
public class MessagingConfigurationCondition extends AnyNestedCondition { | ||
|
||
public MessagingConfigurationCondition() { | ||
super(ConfigurationPhase.PARSE_CONFIGURATION); | ||
} | ||
|
||
@ConditionalOnProperty(prefix = "dina.messaging", name = "isConsumer", havingValue = "true") | ||
static class Value1Condition { | ||
} | ||
|
||
@ConditionalOnProperty(prefix = "dina.messaging", name = "isProducer", havingValue = "true") | ||
static class Value2Condition { | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
dina-messaging/src/main/java/ca/gc/aafc/dina/messaging/config/RabbitMQConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ca.gc.aafc.dina.messaging.config; | ||
|
||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; | ||
import org.springframework.amqp.support.converter.MessageConverter; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.inject.Inject; | ||
|
||
/** | ||
* Configuration of RabbitMQ related beans | ||
*/ | ||
@Configuration | ||
@Conditional(MessagingConfigurationCondition.class) | ||
public class RabbitMQConfig { | ||
|
||
private final RabbitMQProperties rmqProps; | ||
|
||
@Inject | ||
public RabbitMQConfig(RabbitMQProperties rmqProps) { | ||
this.rmqProps = rmqProps; | ||
} | ||
|
||
@Bean | ||
protected ConnectionFactory createConnectionFactory() { | ||
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rmqProps.getHost()); | ||
cachingConnectionFactory.setUsername(rmqProps.getUsername()); | ||
cachingConnectionFactory.setPassword(rmqProps.getPassword()); | ||
|
||
if(rmqProps.getPort() > 0) { | ||
cachingConnectionFactory.setPort(rmqProps.getPort()); | ||
} | ||
|
||
return cachingConnectionFactory; | ||
} | ||
|
||
@Bean | ||
protected MessageConverter createMessageConverter() { | ||
return new Jackson2JsonMessageConverter(); | ||
} | ||
|
||
@Bean | ||
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { | ||
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); | ||
rabbitTemplate.setMessageConverter(createMessageConverter()); | ||
|
||
return rabbitTemplate; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
dina-messaging/src/main/java/ca/gc/aafc/dina/messaging/config/RabbitMQProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ca.gc.aafc.dina.messaging.config; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.validation.annotation.Validated; | ||
|
||
/** | ||
* Loaded from application.yml | ||
*/ | ||
@ConfigurationProperties(prefix = "rabbitmq") | ||
@Component | ||
@Getter | ||
@Setter | ||
@Validated | ||
public class RabbitMQProperties { | ||
|
||
private String host; | ||
private String username; | ||
private String password; | ||
private int port; | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
dina-messaging/src/test/java/ca/gc/aafc/dina/messaging/ConfigLoadingIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ca.gc.aafc.dina.messaging; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import ca.gc.aafc.dina.messaging.config.RabbitMQProperties; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
||
import javax.inject.Inject; | ||
|
||
@SpringBootTest | ||
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) | ||
public class ConfigLoadingIT { | ||
|
||
@Inject | ||
private RabbitMQProperties rabbitMQProps; | ||
|
||
@Test | ||
public void validateConfig() { | ||
assertEquals("localhost", rabbitMQProps.getHost()); | ||
assertNotEquals(0, rabbitMQProps.getPort()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
rabbitmq: | ||
host: localhost | ||
username: guest | ||
password: guest | ||
port: 15672 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters