Solace JMS队列接收JSON消息失败排查:队列已存在警告问题
Hey there! Let's break down your two issues and fix them step by step:
1. Fixing the "Queue already exists" warning
The root cause here is how you're trying to access the queue. You've set connectionFactory.setDynamicDurables(true);, which tells the Solace client to dynamically create the queue if it doesn't exist. But since you already created this queue manually on the Solace Broker, calling session.createQueue(QUEUE_NAME) sends a request to create the queue again—hence the 400 warning.
Here's how to fix it:
- If the queue is pre-configured on the Broker (which it sounds like it is), remove the
connectionFactory.setDynamicDurables(true);line (the default value isfalse, which is what you want for pre-existing queues). - Then, reference the existing queue using Solace's proper queue URI format when creating it in your code:
This tells the client to use the existing queue instead of trying to create a new one.Queue queue = session.createQueue(String.format("queue://%s", QUEUE_NAME));
2. Fixing the missing JSON messages
Your message type check is targeting the wrong class, which is why you're not seeing the JSON content properly. SolaceMsg is an internal Solace implementation class—your JSON messages are almost certainly being sent as standard JMS TextMessage instances (or BytesMessage if using binary serialization).
Let's adjust your message listener to handle JSON correctly:
messageConsumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { try { if (message instanceof TextMessage) { // Extract the JSON string from the TextMessage TextMessage textMessage = (TextMessage) message; String jsonPayload = textMessage.getText(); System.out.printf("Received JSON payload:%n%s%n", jsonPayload); // Deserialize the JSON into your POJO (using Jackson as an example) ObjectMapper objectMapper = new ObjectMapper(); YourCustomerPOJO customerData = objectMapper.readValue(jsonPayload, YourCustomerPOJO.class); System.out.printf("Deserialized Customer Data: ID=%s, Name=%s%n", customerData.getCustomerDetails().getCustomerID(), customerData.getCustomerDetails().getCustomerName()); } else { System.out.println("Received non-text message, dumping content:"); System.out.printf("%s%n", SolJmsUtility.dumpMessage(message)); } message.acknowledge(); latch.countDown(); // Unblock the main thread after first message } catch (JMSException | JsonProcessingException ex) { System.err.println("Error processing incoming message:"); ex.printStackTrace(); } } });
Also, double-check these critical points to ensure messages are reaching your consumer:
- Verify the message producer is sending messages to exactly
test.Request.Q.V01queue in theTEST_VPNVPN. - Confirm your consumer's user account (
testVpn) has read permissions for this queue in the Solace Broker management console. - If your producer uses binary serialization (BytesMessage), adjust the listener to read the byte array and convert it to a string first.
One last note: Your CountDownLatch will stop the consumer after receiving just one message. If you need to keep listening for more messages, remove the latch.await() and latch.countDown() logic, or adjust it to count down after a specific number of messages.
内容的提问来源于stack exchange,提问作者Mike

