Dojo技术问题:如何测试JMS主题是否存在?
Hey there! I've got a few Dojo-specific ways to help you verify if that JMS topic path/to/some/topic actually exists. Let's break them down:
Method 1: Test with a Temporary Subscription + Publish
This is a reliable, non-intrusive way to check if the topic is active (either in Dojo's client-side topic system or backed by a real JMS server). The idea is to send a test message to the topic and listen for it—if you receive the message, the topic exists.
// Import dojo/topic first (if using AMD) // require(['dojo/topic'], (topic) => { ... }); // Create a one-time temporary subscriber const testSubscription = topic.subscribe('path/to/some/topic', (testMsg) => { console.log('✅ Topic exists! Received test message:', testMsg); // Clean up the subscription immediately after receiving the message testSubscription.remove(); }); // Publish a test message to the target topic topic.publish('path/to/some/topic', 'Dojo topic verification test'); // Set a timeout to handle cases where no message is received setTimeout(() => { testSubscription.remove(); // Clean up the subscription console.log('❌ Topic likely does NOT exist - no test message received within timeout window'); }, 2000); // Adjust timeout based on your app's latency
Method 2: Check Dojo's Internal Topic Registry (Dojo 1.x Only)
If you're working with Dojo 1.x, the dojo/topic module maintains an internal registry of all topics that have active subscriptions. You can directly check this registry, but note this relies on an internal API (so it might break in future versions):
// Only valid for Dojo 1.x if (topic._topics && Object.prototype.hasOwnProperty.call(topic._topics, 'path/to/some/topic')) { console.log('✅ Topic found in Dojo\'s internal subscription registry'); } else { console.log('❌ Topic not found in Dojo\'s internal registry (no active subscriptions)'); }
Important: This only checks if there are active subscriptions to the topic in the client-side Dojo app—not whether the underlying JMS topic exists on the server. Use this for client-side verification only.
Method 3: Validate Against the JMS Server (with dojox/jms)
If you're using dojox/jms to connect to a real JMS server, you can directly attempt to create a reference to the topic. Most JMS implementations will throw an error if the topic doesn't exist on the server:
// Import dojox/jms first // require(['dojox/jms'], (jms) => { ... }); // Establish your JMS connection (adjust config to match your server) const connection = jms.connect({ url: 'your-jms-server-url', username: 'your-username', password: 'your-password', // Other JMS provider-specific config }); // Create a session to interact with the server const session = connection.createSession(false, jms.Session.AUTO_ACKNOWLEDGE); try { // Attempt to create a reference to the target topic const targetTopic = session.createTopic('path/to/some/topic'); console.log('✅ JMS topic exists on the server'); } catch (error) { console.error('❌ Failed to access JMS topic - it likely does not exist:', error); } finally { // Clean up resources session.close(); connection.close(); }
Pick the method that fits your use case: use Method 1 for general verification, Method 2 for quick client-side checks (Dojo 1.x), and Method 3 if you need to validate the actual JMS server topic.
内容的提问来源于stack exchange,提问作者Kellen Stuart

