我使用Spring Boot + Apache Camel + Apache Camel RabbitMQ开发了一个简单的生产者-消费者应用程序。我使用了旧的URI格式(现在已弃用),它可以正常工作。但是,当我使用新的URI格式更改语法时,它显示以下错误:-
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_232]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_232]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_232]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_232]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_232]
at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_232]
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.4.3.jar:5.4.3]
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62) ~[amqp-client-5.4.3.jar:5.4.3]
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:156) ~[amqp-client-5.4.3.jar:5.4.3]
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1095) ~[amqp-client-5.4.3.jar:5.4.3]
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1054) ~[amqp-client-5.4.3.jar:5.4.3]
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1012) ~[amqp-client-5.4.3.jar:5.4.3]
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1201) ~[amqp-client-5.4.3.jar:5.4.3]
at org.apache.camel.component.rabbitmq.RabbitMQEndpoint.connect(RabbitMQEndpoint.java:221) ~[camel-rabbitmq-3.0.0.jar:3.0.0]
at org.apache.camel.component.rabbitmq.RabbitMQConsumer.openConnection(RabbitMQConsumer.java:64) ~[camel-rabbitmq-3.0.0.jar:3.0.0]
at org.apache.camel.component.rabbitmq.RabbitMQConsumer.getConnection(RabbitMQConsumer.java:73) ~[camel-rabbitmq-3.0.0.jar:3.0.0]
at org.apache.camel.component.rabbitmq.RabbitConsumer.reconnect(RabbitConsumer.java:316) ~[camel-rabbitmq-3.0.0.jar:3.0.0]
at org.apache.camel.component.rabbitmq.RabbitMQConsumer$StartConsumerCallable.call(RabbitMQConsumer.java:218) [camel-rabbitmq-3.0.0.jar:3.0.0]
at org.apache.camel.component.rabbitmq.RabbitMQConsumer$StartConsumerCallable.call(RabbitMQConsumer.java:198) [camel-rabbitmq-3.0.0.jar:3.0.0]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_232]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_232]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_232]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_232]
2019-12-24 11:35:01.553 INFO 8269 --- [-QUEUE.exchange] o.a.c.component.rabbitmq.RabbitConsumer : Attempting to open a new rabbitMQ channel
2019-12-24 11:35:01.557 INFO 8269 --- [-QUEUE.exchange] o.a.c.c.rabbitmq.RabbitMQConsumer : Connection failed, will retry in 5000ms
这是我的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.arun.learning</groupId>
<artifactId>producer-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath />
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-boot-starter</artifactId>
<version>2.24.2</version> </dependency> -->
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-spring-boot-starter -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.0.0-RC3</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-rabbitmq</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</project>
这是我的Spring Boot Application主类:-
@SpringBootApplication
public class ProducerConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerConsumerApplication.class, args);
}
}
我的 RabbitConfig.java 类
@Component
public class RabbitMQConfig extends RouteBuilder{
JacksonDataFormat jacksonDataFormat = new JacksonDataFormat(User.class);
@Override
public void configure() throws Exception {
//Old URI which is absolutely working fine...
// from("direct:rabbitMQStart").id("rabbitRoute").marshal(jacksonDataFormat)
// .to("rabbitmq://localhost:5672/TEST-QUEUE.exchange?autoDelete=false&routingKey=myRoute1").end();
//
// from("rabbitmq://localhost:5672/TEST-QUEUE.exchange?queue=javainuse.queue&autoDelete=false&routingKey=myRoute1").unmarshal(jacksonDataFormat)
// .to("bean:io.arun.learning.camel.service.UserService?method=addUser");
//
// New URI format which is not working....
from("direct:rabbitMQStart").id("rabbitRoute").marshal(jacksonDataFormat)
.to("rabbitmq:TEST-QUEUE.exchange?autoDelete=false");
from("rabbitmq:TEST-QUEUE.exchange?queue=javainuse.queue&autoDelete=false").unmarshal(jacksonDataFormat)
.to("bean:io.arun.learning.camel.service.UserService?method=addUser");
}
}
我的 UserController.java 类
@RestController
public class UserController {
@Produce(uri = "direct:rabbitMQStart")
private ProducerTemplate producer;
@PostMapping("/users")
public String pushUserIntoQueue(@RequestBody User user) {
Future<Object> status = producer.asyncSendBody(producer.getDefaultEndpoint(), user);
return status.toString();
}
@GetMapping("/users")
public List<UserDto> getUsersFromQueue() {
return UserService.getUsers();
}
}
我的 UserDto.java 类
public class UserDto {
private LocalDateTime receivedAt;
private User user;
public UserDto() {
// TODO Auto-generated constructor stub
}
public UserDto(LocalDateTime receivedAt, User user) {
super();
this.receivedAt = receivedAt;
this.user = user;
}
public LocalDateTime getReceivedAt() {
return receivedAt;
}
public void setReceivedAt(LocalDateTime receivedAt) {
this.receivedAt = receivedAt;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
我的 User.java 类
public class User {
private int id;
private String name;
private int age;
public User() {
}
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
我的 UserService.java 类
public class UserService {
public static List<UserDto> users = new ArrayList<UserDto>();
public void addUser(User user) {
users.add(new UserDto(LocalDateTime.now(), user));
System.out.println("Received User:" + user);
}
public static List<UserDto> getUsers(){
return users;
}
}
有人可以帮助我吗?预先感谢!
答案 0 :(得分:1)
我终于得到了输出。在使用新的URI之前,我们必须将ConnectionFactory注册到spring容器中,如下所示:-
@SpringBootApplication
public class ProducerConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerConsumerApplication.class, args);
}
@Bean
public ConnectionFactory connectionFactory() {
ConnectionFactory con = new ConnectionFactory();
con.setHost("localhost");
return con;
}
}
谢谢!希望对您有所帮助!