关于C# .NET中Solace JNDI/JMS队列订阅的技术求助
Let's walk through how to implement a JNDI-compliant JMS subscription for Solace in .NET—since the direct Session.Connect approach doesn't fit your vendor's requirements. Here's a practical, step-by-step guide with working code:
Prerequisites
First, make sure you have the Solace .NET JMS API installed (via NuGet or direct download) — this package includes all the JNDI and JMS classes needed for .NET.
Step 1: Configure JNDI Connection Properties
Start by setting up the properties to initialize your JNDI context, which will connect to the Solace broker. These include broker addresses, credentials, and the Solace-specific JNDI factory class:
var jndiProps = new Hashtable(); // Solace's JNDI Initial Context Factory class jndiProps[Context.INITIAL_CONTEXT_FACTORY] = "com.solacesystems.jndi.SolJNDIInitialContextFactory"; // Solace broker URL (use tcp:// for plaintext, tcps:// for TLS) jndiProps[Context.PROVIDER_URL] = "tcp://your-solace-broker-host:55555"; // Broker authentication credentials jndiProps[Context.SECURITY_PRINCIPAL] = "your-username"; jndiProps[Context.SECURITY_CREDENTIALS] = "your-password";
Step 2: Initialize JNDI Context & Lookup Resources
Use the properties above to create an InitialContext, then look up the pre-configured Connection Factory and Queue (using the JMS target name provided by your vendor):
IContext jndiContext = new InitialContext(jndiProps); // Look up the Connection Factory from Solace's JNDI registry IConnectionFactory connectionFactory = (IConnectionFactory)jndiContext.Lookup("VendorProvidedConnectionFactoryName"); // Look up the target Queue using its JNDI name IQueue targetQueue = (IQueue)jndiContext.Lookup("VendorProvidedQueueJNDI");
Step 3: Create JMS Connection, Session & Consumer
Now set up the core JMS components. Since you have a SubscriptionId, we'll tie it to the consumer (Solace supports attaching subscription IDs via consumer properties):
// Create and start the JMS connection IConnection connection = connectionFactory.CreateConnection(); connection.Start(); // Create a non-transacted session with auto-acknowledge mode ISession session = connection.CreateSession(false, AcknowledgeMode.AutoAcknowledge); // Create a consumer for the target queue IMessageConsumer queueConsumer = session.CreateConsumer(targetQueue); // Attach your provided SubscriptionId to the consumer queueConsumer.SetStringProperty("SubscriptionId", "YourReceivedSubscriptionId");
Step 4: Handle Incoming Messages
Choose between asynchronous message listening (recommended for most use cases) or synchronous receive:
Option 1: Asynchronous Listener
queueConsumer.MessageListener += (sender, args) => { IMessage incomingMessage = args.Message; try { // Process the message content string messageContent = incomingMessage.Body.ToString(); Console.WriteLine($"Received message: {messageContent}"); // Auto-acknowledge is enabled, so no explicit ack needed here } finally { // Always dispose the message to free resources incomingMessage.Dispose(); } };
Option 2: Synchronous Receive
while (true) { // Wait 1 second for a message (adjust timeout as needed) IMessage incomingMessage = queueConsumer.Receive(1000); if (incomingMessage != null) { try { Console.WriteLine($"Received message: {incomingMessage.Body.ToString()}"); } finally { incomingMessage.Dispose(); } } }
Step 5: Clean Up Resources
Always close JMS resources when your application is done to avoid broker-side leaks:
queueConsumer.Close(); session.Close(); connection.Close(); jndiContext.Close();
Key Notes
- Double-check that the JNDI names for the Connection Factory and Queue match exactly what your vendor has configured on the Solace broker.
- If your
SubscriptionIdis for a durable subscription, useCreateDurableConsumerinstead:IMessageConsumer durableConsumer = session.CreateDurableConsumer(targetQueue, "YourReceivedSubscriptionId"); - Ensure your Solace broker has permissions set to allow JNDI lookups for the specified resources.
内容的提问来源于stack exchange,提问作者Greg

