Skip to content

Commit 5fbb853

Browse files
committed
add rabbit mq
1 parent 7ac3ed0 commit 5fbb853

39 files changed

Lines changed: 602 additions & 97 deletions

File tree

JavaTMP-SpringBoot-Modules/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ out/
3535
### Default Logs ###
3636
/logs/
3737
logs/
38+
39+
*.hprof
40+
java_pid*

JavaTMP-SpringBoot-Modules/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,9 @@ subprojects {
9999
enabled = false
100100
}
101101
}
102+
103+
test {
104+
exclude '**/*'
105+
}
106+
107+
test.onlyIf { false == true }

JavaTMP-SpringBoot-Modules/settings.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ include 'spring-boot-miscellaneous:spring-boot-testing'
4848

4949
include 'spring-boot-cache:spring-boot-cache-simple'
5050

51-
include 'spring-boot-webservices:spring-boot-soap-producer'
52-
include 'spring-boot-webservices:spring-boot-soap-consumer'
53-
include 'spring-boot-webservices:spring-boot-web-services'
54-
include 'spring-boot-webservices:spring-boot-web-services-client'
51+
//include 'spring-boot-webservices:spring-boot-soap-producer'
52+
//include 'spring-boot-webservices:spring-boot-soap-consumer'
53+
//include 'spring-boot-webservices:spring-boot-web-services'
54+
//include 'spring-boot-webservices:spring-boot-web-services-client'
55+
56+
include 'spring-boot-integration:spring-boot-rabbitmq'
57+
include 'spring-boot-integration:spring-boot-rabbitmq-config'
5558

5659
rootProject.children.each {project ->
5760
println "child project is : ${project.name}"

JavaTMP-SpringBoot-Modules/spring-boot-data/spring-boot-jpa-multi-advance/src/test/java/com/javatmp/demo/jpa/SpringBootDemoMultiDatasourceJpaApplicationTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.javatmp.demo.jpa.repository.second.SecondMultiTableRepository;
77
import lombok.extern.slf4j.Slf4j;
88
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.BeanUtils;
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.boot.test.context.SpringBootTest;
1112

@@ -18,16 +19,15 @@ public class SpringBootDemoMultiDatasourceJpaApplicationTests {
1819
private PrimaryMultiTableRepository primaryRepo;
1920
@Autowired
2021
private SecondMultiTableRepository secondRepo;
21-
@Autowired
22-
private Snowflake snowflake;
22+
2323

2424
@Test
2525
public void testInsert() {
26-
PrimaryMultiTable primary = new PrimaryMultiTable(snowflake.nextId(), "测试名称-1");
26+
PrimaryMultiTable primary = new PrimaryMultiTable(1L, "测试名称-1");
2727
primaryRepo.save(primary);
2828

2929
SecondMultiTable second = new SecondMultiTable();
30-
BeanUtil.copyProperties(primary, second);
30+
BeanUtils.copyProperties(primary, second);
3131
secondRepo.save(second);
3232
}
3333

@@ -38,7 +38,7 @@ public void testUpdate() {
3838
primaryRepo.save(primary);
3939

4040
SecondMultiTable second = new SecondMultiTable();
41-
BeanUtil.copyProperties(primary, second);
41+
BeanUtils.copyProperties(primary, second);
4242
secondRepo.save(second);
4343
});
4444
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Spring Boot Integration Modules
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
bootJar {
3+
enabled = true
4+
}
5+
6+
7+
dependencies {
8+
implementation 'org.springframework.boot:spring-boot-starter-amqp'
9+
testImplementation 'org.springframework.amqp:spring-rabbit-test'
10+
implementation 'com.fasterxml.jackson.core:jackson-databind'
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Spring Boot Rabbitmq Module
2+
3+
## References
4+
- [https://spring.io/guides/gs/messaging-rabbitmq/](https://spring.io/guides/gs/messaging-rabbitmq/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.javatmp.demo.mq.config;
2+
3+
import org.springframework.amqp.core.*;
4+
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
5+
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
6+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
7+
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
8+
import org.springframework.amqp.support.converter.MessageConverter;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
@Configuration
14+
public class RabbitMQConfig {
15+
16+
@Value("${spring.rabbitmq.queue}")
17+
private String queue;
18+
19+
@Value("${spring.rabbitmq.exchange}")
20+
private String exchange;
21+
22+
@Value("${spring.rabbitmq.routingkey}")
23+
private String routingKey;
24+
25+
@Value("${spring.rabbitmq.username}")
26+
private String username;
27+
28+
@Value("${spring.rabbitmq.password}")
29+
private String password;
30+
31+
@Value("${spring.rabbitmq.host}")
32+
private String host;
33+
34+
@Bean
35+
Queue queue() {
36+
return new Queue(queue, true);
37+
}
38+
39+
@Bean
40+
Exchange myExchange() {
41+
return ExchangeBuilder.directExchange(exchange).durable(true).build();
42+
}
43+
44+
@Bean
45+
Binding binding() {
46+
return BindingBuilder
47+
.bind(queue())
48+
.to(myExchange())
49+
.with(routingKey)
50+
.noargs();
51+
}
52+
53+
@Bean
54+
public ConnectionFactory connectionFactory() {
55+
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(host);
56+
cachingConnectionFactory.setUsername(username);
57+
cachingConnectionFactory.setPassword(password);
58+
return cachingConnectionFactory;
59+
}
60+
61+
@Bean
62+
public MessageConverter jsonMessageConverter() {
63+
return new Jackson2JsonMessageConverter();
64+
}
65+
66+
@Bean
67+
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
68+
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
69+
rabbitTemplate.setMessageConverter(jsonMessageConverter());
70+
return rabbitTemplate;
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.javatmp.demo.mq.consumer;
2+
3+
import com.javatmp.demo.mq.config.RabbitMQConfig;
4+
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
@EnableRabbit
9+
@SpringBootApplication(scanBasePackageClasses = {RabbitMQConfig.class, ConsumerServiceApplication.class})
10+
public class ConsumerServiceApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(ConsumerServiceApplication.class, args);
14+
}
15+
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.javatmp.demo.mq.consumer;
2+
3+
import com.javatmp.demo.mq.domain.User;
4+
import org.springframework.amqp.rabbit.annotation.RabbitListener;
5+
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
6+
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
7+
import org.springframework.stereotype.Component;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
@Component
12+
public class RabbitMqReceiver implements RabbitListenerConfigurer {
13+
14+
private static final Logger logger = LoggerFactory.getLogger(RabbitMqReceiver.class);
15+
16+
@RabbitListener(queues = "${spring.rabbitmq.queue}")
17+
public void receivedMessage(User user) {
18+
19+
logger.info("User Details Received is.. " + user);
20+
}
21+
22+
@Override
23+
public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
24+
25+
}
26+
}

0 commit comments

Comments
 (0)