Java RabbitMQ 在局部变量中存储消费消息不起作用
Java RabbitMQ storing a consumed message in local variable not working
我要完成的任务包括两个 classes,一个 class 是 Student,另一个是 University学生服务。class 学生 应该填写表格并通过 RabbitMQ 队列向 class 大学学生服务。一旦学生服务检查表格是否有效,他们应该向 Student class 发送一个答案,以便它充当一些某种 RPC。
我尝试做的是,我尝试将 大学生服务 收到的消息存储在局部变量类型 String 中并检查它的有效性,以便我可以发回消息给 Student class 让它知道答案是什么。
问题是我无法存储消息,我尝试使用 setter 但它不起作用,普通运算符 = 存储也不起作用。
代码:
Class 学生
public static void main(String[] args) throws IOException, TimeoutException {
String check = "";
while(!check.equals("End")) {
Scanner input = new Scanner(System.in);
System.out.println("Input your name and surname:");
String imePrezime = input.nextLine();
System.out.println("\nInput your ordinal number:");
String redniBr = input.nextLine();
System.out.println("\nInput the year you applied:");
String godinaUpisa = input.nextLine();
System.out.println("\nInput the name of the subject:");
String nazivPredmeta = input.nextLine();
System.out.println("\nInput semester:");
String semestar = input.nextLine();
System.out.println("\nInput current year:");
String studijskaGodina = input.nextLine();
String finalnaPoruka = imePrezime + " " + redniBr + " " + godinaUpisa + " " + nazivPredmeta + " " + semestar + " " + studijskaGodina;
ConnectionFactory factory = new ConnectionFactory();
try (Connection connection = factory.newConnection()){
Channel channel = connection.createChannel();
channel.queueDeclare("Apply",false,false,false,null);
String message = finalnaPoruka;
channel.basicPublish("","Apply",false,null,message.getBytes());
System.out.println("The message has been delivered.\n");
}
}
}
}
正如你在上面看到的,在这个 class 我只是发送了一个字符串。
Class大学生服务
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Connection;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
public class Sluzba {
static String globalMessage = "";
public static void setMsg(String arg) {
globalMessage = arg;
}
public static void main(String[] args) throws IOException, TimeoutException {
int check = 0;
ConnectionFactory factory = new ConnectionFactory();
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("Apply",false,false,false,null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
setMsg(message);
System.out.println("New application has arrived '" + message + "'");
};
channel.basicConsume("Apply", true, deliverCallback, consumerTag -> { });
System.out.println("Global message " + globalMessage + " stored");
try (Connection connectionReply = factory.newConnection()){
Channel channelReply = connection.createChannel();
channelReply.queueDeclare("Answer",false,false,false,null);
String messageReply = "Test";
channel.basicPublish("","Answer",false,null,messageReply.getBytes());
System.out.println("Answer has been delivered.\n");
}
}
}
在上面的 class 中,我正在尝试检查它是否有效(我仍然没有完成代码),总而言之,我想保存收到的消息,检查有效性并尝试发送返回给 Student 的答案,以便它可以处理结果。
System.out.println("Global message " + globalMessage + " stored");
一旦我存储它,这应该会打印相同的消息,但它不起作用,并且它不会给出任何编译错误。
侧面 question:Is 这是执行此 RPC 模拟交换的正确方法还是我有一个坏主意?
谢谢大家的宝贵时间。
这似乎是引用传递类型的问题。您在 setMsg 方法调用中传递消息引用,该引用指向在 driverCallBack 中创建的消息的内存位置。这意味着 globalMessage 只是指向同一个消息内存块。
现在,一旦 driverCallBack 块被执行,消息内存块就会被释放,现在 globalMessage 指向不存在的块,因此值丢失。
所以你可以用new String(arg)初始化globalMessage,这样value就被复制到一个新的内存块,不再依赖于消息内存块。
public static void setMsg(String arg) {
globalMessage = new String(arg);
}
我要完成的任务包括两个 classes,一个 class 是 Student,另一个是 University学生服务。class 学生 应该填写表格并通过 RabbitMQ 队列向 class 大学学生服务。一旦学生服务检查表格是否有效,他们应该向 Student class 发送一个答案,以便它充当一些某种 RPC。
我尝试做的是,我尝试将 大学生服务 收到的消息存储在局部变量类型 String 中并检查它的有效性,以便我可以发回消息给 Student class 让它知道答案是什么。
问题是我无法存储消息,我尝试使用 setter 但它不起作用,普通运算符 = 存储也不起作用。
代码:
Class 学生
public static void main(String[] args) throws IOException, TimeoutException {
String check = "";
while(!check.equals("End")) {
Scanner input = new Scanner(System.in);
System.out.println("Input your name and surname:");
String imePrezime = input.nextLine();
System.out.println("\nInput your ordinal number:");
String redniBr = input.nextLine();
System.out.println("\nInput the year you applied:");
String godinaUpisa = input.nextLine();
System.out.println("\nInput the name of the subject:");
String nazivPredmeta = input.nextLine();
System.out.println("\nInput semester:");
String semestar = input.nextLine();
System.out.println("\nInput current year:");
String studijskaGodina = input.nextLine();
String finalnaPoruka = imePrezime + " " + redniBr + " " + godinaUpisa + " " + nazivPredmeta + " " + semestar + " " + studijskaGodina;
ConnectionFactory factory = new ConnectionFactory();
try (Connection connection = factory.newConnection()){
Channel channel = connection.createChannel();
channel.queueDeclare("Apply",false,false,false,null);
String message = finalnaPoruka;
channel.basicPublish("","Apply",false,null,message.getBytes());
System.out.println("The message has been delivered.\n");
}
}
}
}
正如你在上面看到的,在这个 class 我只是发送了一个字符串。
Class大学生服务
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Connection;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
public class Sluzba {
static String globalMessage = "";
public static void setMsg(String arg) {
globalMessage = arg;
}
public static void main(String[] args) throws IOException, TimeoutException {
int check = 0;
ConnectionFactory factory = new ConnectionFactory();
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("Apply",false,false,false,null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
setMsg(message);
System.out.println("New application has arrived '" + message + "'");
};
channel.basicConsume("Apply", true, deliverCallback, consumerTag -> { });
System.out.println("Global message " + globalMessage + " stored");
try (Connection connectionReply = factory.newConnection()){
Channel channelReply = connection.createChannel();
channelReply.queueDeclare("Answer",false,false,false,null);
String messageReply = "Test";
channel.basicPublish("","Answer",false,null,messageReply.getBytes());
System.out.println("Answer has been delivered.\n");
}
}
}
在上面的 class 中,我正在尝试检查它是否有效(我仍然没有完成代码),总而言之,我想保存收到的消息,检查有效性并尝试发送返回给 Student 的答案,以便它可以处理结果。
System.out.println("Global message " + globalMessage + " stored");
一旦我存储它,这应该会打印相同的消息,但它不起作用,并且它不会给出任何编译错误。
侧面 question:Is 这是执行此 RPC 模拟交换的正确方法还是我有一个坏主意?
谢谢大家的宝贵时间。
这似乎是引用传递类型的问题。您在 setMsg 方法调用中传递消息引用,该引用指向在 driverCallBack 中创建的消息的内存位置。这意味着 globalMessage 只是指向同一个消息内存块。
现在,一旦 driverCallBack 块被执行,消息内存块就会被释放,现在 globalMessage 指向不存在的块,因此值丢失。
所以你可以用new String(arg)初始化globalMessage,这样value就被复制到一个新的内存块,不再依赖于消息内存块。
public static void setMsg(String arg) {
globalMessage = new String(arg);
}