如何使用Java将错误消息移动到Azure死信队列?
我们使用Azure服务总线队列在不同系统之间交换消息。我们希望使用Java代码将无效消息移动到死信队列如何使用Java将错误消息移动到Azure死信队列?,java,azure,azureservicebus,azure-servicebus-queues,Java,Azure,Azureservicebus,Azure Servicebus Queues,我们使用Azure服务总线队列在不同系统之间交换消息。我们希望使用Java代码将无效消息移动到死信队列 我可以将消息移动到主队列,但不能移动到死信队列。我试图将队列名称命名为“BasicQueue/$deadletterqueue”,但我得到了错误。Azure Service bus运行时可能会自动将消息移动到死信队列的原因有很多,例如: 超过最大交付计数(启用时默认为10) 超过消息时间间隔(TTL) 等等, 在中查找更多详细信息,包括将邮件移动到死信队列的原因的完整列表 此外,应用程序还
我可以将消息移动到主队列,但不能移动到死信队列。我试图将队列名称命名为“BasicQueue/$deadletterqueue”,但我得到了错误。Azure Service bus运行时可能会自动将消息移动到死信队列的原因有很多,例如:
- 超过最大交付计数(启用时默认为10)李>
- 超过消息时间间隔(TTL)李>
- 等等,李>
DeadLetterReason
DeadLetterErrorDescription
--Java版本--
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
<dependencies>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-servicebus -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-servicebus</artifactId>
<version>2.0.0-preview1</version>
</dependency>
</dependencies>
import java.time.Duration;
import java.util.Scanner;
import com.microsoft.azure.servicebus.MessageHandlerOptions;
import com.microsoft.azure.servicebus.QueueClient;
import com.microsoft.azure.servicebus.ReceiveMode;
import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
public class Main {
public static void main(String[] args)
{
final String CONNECTION_STRING = "{Azure Service Bus Connection String}";
final String QUEUE_NAME = "{Azure Service Bus Queue Name}";
try {
moveMessageToDlq(CONNECTION_STRING, QUEUE_NAME);
}catch (Exception ex) {
System.out.println(String.format("An exception occurred. Details: %s.", ex.toString()));
}
}
private static void moveMessageToDlq(String connectionString, String queueName)
throws ServiceBusException, InterruptedException {
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(connectionString, queueName);
QueueClient queueClient = new QueueClient(connectionStringBuilder, ReceiveMode.PEEKLOCK);
MessageHandler messageHandler = new MessageHandler(queueClient);
MessageHandlerOptions messageHandlerOptions = new MessageHandlerOptions(
1,
false,
Duration.ofMinutes(1));
queueClient.registerMessageHandler(messageHandler, messageHandlerOptions);
final String QUIT_COMMAND_NAME = "quit";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println(String.format("Enter '%s' and press <ENTER> to exit...", QUIT_COMMAND_NAME));
String input = scanner.nextLine();
if (input.equalsIgnoreCase(QUIT_COMMAND_NAME)){
System.out.println("Exiting...");
break;
}
}
scanner.close();
queueClient.close();
}
}
import java.util.concurrent.CompletableFuture;
import com.microsoft.azure.servicebus.ExceptionPhase;
import com.microsoft.azure.servicebus.IMessage;
import com.microsoft.azure.servicebus.IMessageHandler;
import com.microsoft.azure.servicebus.IQueueClient;
public class MessageHandler implements IMessageHandler {
private final IQueueClient _client;
public MessageHandler(IQueueClient client) {
if (client == null){
throw new IllegalArgumentException("Queue client cannot be null.");
}
_client = client;
}
@Override
public CompletableFuture<Void> onMessageAsync(IMessage message) {
System.out.println(String.format("Received message id '%s' (DeliveryCount=%d).",
message.getMessageId(),
message.getDeliveryCount()));
if (message.getLabel().equalsIgnoreCase("dlq")){ // Send message to DLQ if label id dlq
System.out.println("Sending message to the dead letter queue...");
return _client.deadLetterAsync(
message.getLockToken(),
"InvalidMessage", // DeadLetterReason
"Message invalid due to..."); // DeadLetterErrorDescription
}
else { // Otherwise, complete message
System.out.println("Completing message...");
return _client.completeAsync(message.getLockToken());
}
}
@Override
public void notifyException(Throwable throwable, ExceptionPhase exceptionPhase) {
System.out.println("An exception occurred. Details: " + exceptionPhase + "-" + throwable.getMessage());
}
}
---pom.xml的Azure服务总线依赖关系---
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
<dependencies>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-servicebus -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-servicebus</artifactId>
<version>2.0.0-preview1</version>
</dependency>
</dependencies>
import java.time.Duration;
import java.util.Scanner;
import com.microsoft.azure.servicebus.MessageHandlerOptions;
import com.microsoft.azure.servicebus.QueueClient;
import com.microsoft.azure.servicebus.ReceiveMode;
import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
public class Main {
public static void main(String[] args)
{
final String CONNECTION_STRING = "{Azure Service Bus Connection String}";
final String QUEUE_NAME = "{Azure Service Bus Queue Name}";
try {
moveMessageToDlq(CONNECTION_STRING, QUEUE_NAME);
}catch (Exception ex) {
System.out.println(String.format("An exception occurred. Details: %s.", ex.toString()));
}
}
private static void moveMessageToDlq(String connectionString, String queueName)
throws ServiceBusException, InterruptedException {
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(connectionString, queueName);
QueueClient queueClient = new QueueClient(connectionStringBuilder, ReceiveMode.PEEKLOCK);
MessageHandler messageHandler = new MessageHandler(queueClient);
MessageHandlerOptions messageHandlerOptions = new MessageHandlerOptions(
1,
false,
Duration.ofMinutes(1));
queueClient.registerMessageHandler(messageHandler, messageHandlerOptions);
final String QUIT_COMMAND_NAME = "quit";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println(String.format("Enter '%s' and press <ENTER> to exit...", QUIT_COMMAND_NAME));
String input = scanner.nextLine();
if (input.equalsIgnoreCase(QUIT_COMMAND_NAME)){
System.out.println("Exiting...");
break;
}
}
scanner.close();
queueClient.close();
}
}
import java.util.concurrent.CompletableFuture;
import com.microsoft.azure.servicebus.ExceptionPhase;
import com.microsoft.azure.servicebus.IMessage;
import com.microsoft.azure.servicebus.IMessageHandler;
import com.microsoft.azure.servicebus.IQueueClient;
public class MessageHandler implements IMessageHandler {
private final IQueueClient _client;
public MessageHandler(IQueueClient client) {
if (client == null){
throw new IllegalArgumentException("Queue client cannot be null.");
}
_client = client;
}
@Override
public CompletableFuture<Void> onMessageAsync(IMessage message) {
System.out.println(String.format("Received message id '%s' (DeliveryCount=%d).",
message.getMessageId(),
message.getDeliveryCount()));
if (message.getLabel().equalsIgnoreCase("dlq")){ // Send message to DLQ if label id dlq
System.out.println("Sending message to the dead letter queue...");
return _client.deadLetterAsync(
message.getLockToken(),
"InvalidMessage", // DeadLetterReason
"Message invalid due to..."); // DeadLetterErrorDescription
}
else { // Otherwise, complete message
System.out.println("Completing message...");
return _client.completeAsync(message.getLockToken());
}
}
@Override
public void notifyException(Throwable throwable, ExceptionPhase exceptionPhase) {
System.out.println("An exception occurred. Details: " + exceptionPhase + "-" + throwable.getMessage());
}
}
com.microsoft.azure
.Azure Service bus运行时可能会自动将消息移动到死信队列的原因有很多,例如:
- 超过最大交付计数(启用时默认为10)李>
- 超过消息时间间隔(TTL)李>
- 等等,李>
在中查找更多详细信息,包括将邮件移动到死信队列的原因的完整列表
此外,应用程序还可以将消息移动到死信队列。在下面进一步查找示例Java8控制台应用程序代码和pom.xml文件
请注意,将消息移动到死信队列时,Azure Service Bus运行时会自动填充以下属性,最好在应用程序执行相同操作时也提供这些属性:
DeadLetterReason
DeadLetterErrorDescription
--Java版本--
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
<dependencies>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-servicebus -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-servicebus</artifactId>
<version>2.0.0-preview1</version>
</dependency>
</dependencies>
import java.time.Duration;
import java.util.Scanner;
import com.microsoft.azure.servicebus.MessageHandlerOptions;
import com.microsoft.azure.servicebus.QueueClient;
import com.microsoft.azure.servicebus.ReceiveMode;
import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
public class Main {
public static void main(String[] args)
{
final String CONNECTION_STRING = "{Azure Service Bus Connection String}";
final String QUEUE_NAME = "{Azure Service Bus Queue Name}";
try {
moveMessageToDlq(CONNECTION_STRING, QUEUE_NAME);
}catch (Exception ex) {
System.out.println(String.format("An exception occurred. Details: %s.", ex.toString()));
}
}
private static void moveMessageToDlq(String connectionString, String queueName)
throws ServiceBusException, InterruptedException {
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(connectionString, queueName);
QueueClient queueClient = new QueueClient(connectionStringBuilder, ReceiveMode.PEEKLOCK);
MessageHandler messageHandler = new MessageHandler(queueClient);
MessageHandlerOptions messageHandlerOptions = new MessageHandlerOptions(
1,
false,
Duration.ofMinutes(1));
queueClient.registerMessageHandler(messageHandler, messageHandlerOptions);
final String QUIT_COMMAND_NAME = "quit";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println(String.format("Enter '%s' and press <ENTER> to exit...", QUIT_COMMAND_NAME));
String input = scanner.nextLine();
if (input.equalsIgnoreCase(QUIT_COMMAND_NAME)){
System.out.println("Exiting...");
break;
}
}
scanner.close();
queueClient.close();
}
}
import java.util.concurrent.CompletableFuture;
import com.microsoft.azure.servicebus.ExceptionPhase;
import com.microsoft.azure.servicebus.IMessage;
import com.microsoft.azure.servicebus.IMessageHandler;
import com.microsoft.azure.servicebus.IQueueClient;
public class MessageHandler implements IMessageHandler {
private final IQueueClient _client;
public MessageHandler(IQueueClient client) {
if (client == null){
throw new IllegalArgumentException("Queue client cannot be null.");
}
_client = client;
}
@Override
public CompletableFuture<Void> onMessageAsync(IMessage message) {
System.out.println(String.format("Received message id '%s' (DeliveryCount=%d).",
message.getMessageId(),
message.getDeliveryCount()));
if (message.getLabel().equalsIgnoreCase("dlq")){ // Send message to DLQ if label id dlq
System.out.println("Sending message to the dead letter queue...");
return _client.deadLetterAsync(
message.getLockToken(),
"InvalidMessage", // DeadLetterReason
"Message invalid due to..."); // DeadLetterErrorDescription
}
else { // Otherwise, complete message
System.out.println("Completing message...");
return _client.completeAsync(message.getLockToken());
}
}
@Override
public void notifyException(Throwable throwable, ExceptionPhase exceptionPhase) {
System.out.println("An exception occurred. Details: " + exceptionPhase + "-" + throwable.getMessage());
}
}
---pom.xml的Azure服务总线依赖关系---
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
<dependencies>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-servicebus -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-servicebus</artifactId>
<version>2.0.0-preview1</version>
</dependency>
</dependencies>
import java.time.Duration;
import java.util.Scanner;
import com.microsoft.azure.servicebus.MessageHandlerOptions;
import com.microsoft.azure.servicebus.QueueClient;
import com.microsoft.azure.servicebus.ReceiveMode;
import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
public class Main {
public static void main(String[] args)
{
final String CONNECTION_STRING = "{Azure Service Bus Connection String}";
final String QUEUE_NAME = "{Azure Service Bus Queue Name}";
try {
moveMessageToDlq(CONNECTION_STRING, QUEUE_NAME);
}catch (Exception ex) {
System.out.println(String.format("An exception occurred. Details: %s.", ex.toString()));
}
}
private static void moveMessageToDlq(String connectionString, String queueName)
throws ServiceBusException, InterruptedException {
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(connectionString, queueName);
QueueClient queueClient = new QueueClient(connectionStringBuilder, ReceiveMode.PEEKLOCK);
MessageHandler messageHandler = new MessageHandler(queueClient);
MessageHandlerOptions messageHandlerOptions = new MessageHandlerOptions(
1,
false,
Duration.ofMinutes(1));
queueClient.registerMessageHandler(messageHandler, messageHandlerOptions);
final String QUIT_COMMAND_NAME = "quit";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println(String.format("Enter '%s' and press <ENTER> to exit...", QUIT_COMMAND_NAME));
String input = scanner.nextLine();
if (input.equalsIgnoreCase(QUIT_COMMAND_NAME)){
System.out.println("Exiting...");
break;
}
}
scanner.close();
queueClient.close();
}
}
import java.util.concurrent.CompletableFuture;
import com.microsoft.azure.servicebus.ExceptionPhase;
import com.microsoft.azure.servicebus.IMessage;
import com.microsoft.azure.servicebus.IMessageHandler;
import com.microsoft.azure.servicebus.IQueueClient;
public class MessageHandler implements IMessageHandler {
private final IQueueClient _client;
public MessageHandler(IQueueClient client) {
if (client == null){
throw new IllegalArgumentException("Queue client cannot be null.");
}
_client = client;
}
@Override
public CompletableFuture<Void> onMessageAsync(IMessage message) {
System.out.println(String.format("Received message id '%s' (DeliveryCount=%d).",
message.getMessageId(),
message.getDeliveryCount()));
if (message.getLabel().equalsIgnoreCase("dlq")){ // Send message to DLQ if label id dlq
System.out.println("Sending message to the dead letter queue...");
return _client.deadLetterAsync(
message.getLockToken(),
"InvalidMessage", // DeadLetterReason
"Message invalid due to..."); // DeadLetterErrorDescription
}
else { // Otherwise, complete message
System.out.println("Completing message...");
return _client.completeAsync(message.getLockToken());
}
}
@Override
public void notifyException(Throwable throwable, ExceptionPhase exceptionPhase) {
System.out.println("An exception occurred. Details: " + exceptionPhase + "-" + throwable.getMessage());
}
}
com.microsoft.azure
.您可以发布一些代码和错误补丁吗?您可以发布一些代码和错误补丁吗