publish activemq topic in csharp

To publish messages to an ActiveMQ topic in c#, you will need to use the Apache.NMS API library. Below is an example code snippet to publish messages to an ActiveMQ topic:

main.cs
using Apache.NMS;
using Apache.NMS.ActiveMQ;

class Program
{
    static void Main(string[] args)
    {
        // Create a connection string to the ActiveMQ server
        string brokerUri = "tcp://localhost:61616";

        // Create a connection factory
        IConnectionFactory factory = new ConnectionFactory(brokerUri);

        // Create a connection
        using (IConnection connection = factory.CreateConnection())
        {
            // Start the connection
            connection.Start();

            // Create a session
            using (ISession session = connection.CreateSession())
            {
                // Create a topic
                ITopic topic = session.GetTopic("SampleTopic");

                // Create a message producer
                using (IMessageProducer producer = session.CreateProducer(topic))
                {
                    // Create a text message
                    ITextMessage message = producer.CreateTextMessage("Hello, world!");

                    // Send the message
                    producer.Send(message);
                }
            }
        }
    }
}
1131 chars
39 lines

In the code snippet above, we create a connection to the ActiveMQ server using the provided broker URI. We then create a session and a topic to publish our message to. Finally, we create a message producer and send our message to the topic.

gistlibby LogSnag