Skip to content

Commit 286b99b

Browse files
committed
add rabbitmq
1 parent 20f3b91 commit 286b99b

File tree

14 files changed

+135
-35
lines changed

14 files changed

+135
-35
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ target
66
log
77
.idea
88
*.iml
9+
build/
10+
.gradle/

build.gradle

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ subprojects { subproject ->
2828
repositories {
2929
mavenLocal()
3030
mavenCentral()
31-
maven {url "http://maven.oschina.net/content/groups/public/"}
32-
maven {url "http://repos.someok.com/release"}
3331
}
3432

3533
dependencies {
36-
// tag::jetty[]
37-
compile("org.springframework.boot:spring-boot-starter-web")
34+
compile("org.springframework.boot:spring-boot-starter-amqp")
3835
}
3936
}
4037

gradle-consumer/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apply plugin: 'spring-boot'
2+
dependencies {
3+
compile("org.springframework.boot:spring-boot-starter-web")
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
package hello;
2-
3-
/**
4-
* @user tianjun
5-
* @date 16/6/23
6-
*/
1+
package com.ttianjun;
72

83
import org.springframework.boot.SpringApplication;
94
import org.springframework.boot.autoconfigure.SpringBootApplication;
105

6+
/**
7+
* @user tianjun
8+
* @date 16/6/24
9+
*/
1110
@SpringBootApplication
1211
public class Application {
13-
1412
public static void main(String[] args) {
1513
SpringApplication.run(Application.class, args);
1614
}
17-
18-
}
15+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.ttianjun.config;
2+
3+
import com.ttianjun.listen.ListenDemo;
4+
import org.springframework.amqp.core.*;
5+
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
6+
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
7+
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
/**
13+
* @user tianjun
14+
* @date 16/6/24
15+
*/
16+
@Configuration
17+
public class MessageConfig {
18+
@Value("${default-exchange}")
19+
private String exchange;
20+
21+
private final String queueName="queue-fanout";
22+
@Bean
23+
Queue queue() {
24+
return new Queue(queueName, false);
25+
}
26+
27+
@Bean
28+
FanoutExchange exchange() {
29+
return new FanoutExchange(exchange);
30+
}
31+
32+
@Bean
33+
Binding binding(Queue queue, FanoutExchange exchange) {
34+
return BindingBuilder.bind(queue).to(exchange);
35+
}
36+
37+
@Bean
38+
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, ListenDemo listenDemo) {
39+
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
40+
container.setConnectionFactory(connectionFactory);
41+
container.setQueueNames(queueName);
42+
container.setMessageListener(new MessageListenerAdapter(listenDemo,"handleMessage"));
43+
return container;
44+
}
45+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.ttianjun.listen;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
/**
6+
* @user tianjun
7+
* @date 16/6/24
8+
*/
9+
@Component
10+
public class ListenDemo {
11+
public void handleMessage(String message) {
12+
System.out.println(message);
13+
}
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.rabbitmq.host=127.0.0.1
2+
spring.rabbitmq.port=5672
3+
spring.rabbitmq.username=guest
4+
spring.rabbitmq.password=guest
5+
spring.rabbitmq.virtual-host=/
6+
default-exchange=amq.fanout
File renamed without changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package hello;
2+
3+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
10+
11+
@SpringBootApplication
12+
public class Application implements CommandLineRunner {
13+
14+
@Value("${default-exchange}")
15+
private String exchange;
16+
17+
@Autowired
18+
AnnotationConfigApplicationContext context;
19+
20+
@Autowired
21+
RabbitTemplate rabbitTemplate;
22+
23+
public static void main(String[] args) throws InterruptedException {
24+
SpringApplication.run(Application.class, args);
25+
}
26+
27+
@Override
28+
public void run(String... args) throws Exception {
29+
System.out.println("Sending message...");
30+
rabbitTemplate.convertAndSend(exchange,null,"Hello from RabbitMQ!");
31+
context.close();
32+
}
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.rabbitmq.host=127.0.0.1
2+
spring.rabbitmq.port=5672
3+
spring.rabbitmq.username=guest
4+
spring.rabbitmq.password=guest
5+
spring.rabbitmq.virtual-host=/
6+
default-exchange=amq.fanout

0 commit comments

Comments
 (0)