private Procucer(string customerId, string address) { instance = this; //创建工厂 IConnectionFactory _factory = new ConnectionFactory("tcp://127.0.0.1:61616/"); try { //创建连接 IConnection _connection = _factory.CreateConnection(); { //创建会话 ISession session = _connection.CreateSession(); { //创建一个主题 IDestination destination = new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("topic"); //创建生产者 producer = session.CreateProducer(destination); Console.WriteLine("Please enter any key to continue! "); // Console.ReadKey(); Console.WriteLine("Sending: "); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } //Console.ReadLine(); }
static void Main(string[] args) { _factory = new ConnectionFactory("tcp://127.0.0.1:61616/"); try { using (_connection = _factory.CreateConnection()) { using (ISession session = _connection.CreateSession()) { IDestination destination = new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("topic"); IMessageProducer producer = session.CreateProducer(destination); Console.WriteLine("Sending: "); _message = producer.CreateTextMessage("Hello ActiveMQ..."); //发送消息 producer.Send(_message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue); while (true) { var msg = Console.ReadLine(); _message = producer.CreateTextMessage(msg); producer.Send(_message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue); } } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.ReadLine(); }
static void Main(string[] args) { //创建工厂 _factory = new ConnectionFactory("tcp://localhost:61616/"); 11 : try { //创建连接 using (_connection = _factory.CreateConnection()) { //创建会话 using (ISession session = _connection.CreateSession(AcknowledgementMode.AutoAcknowledge)) { //创建一个主题 IDestination destination = new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("topic"); //创建生产者 IMessageProducer producer = session.CreateProducer(destination); int counter = 0; Console.WriteLine("请输入你要发送数据,然后回车!"); while (true) { string msg = Console.ReadLine(); if (msg != string.Empty) { Console.BackgroundColor = ConsoleColor.Blue; Console.Beep(); counter++; //创建一个文本消息 _message = producer.CreateTextMessage("This is .Net AcitveMQ Message...."); //_message2 = producer.CreateTextMessage("<Root><First>Test</First><Root>"); Console.WriteLine("正在发送第{0}组发送数据...........", counter); //发送消息 producer.Send(_message, MsgDeliveryMode.Persistent, MsgPriority.Normal, TimeSpan.MaxValue); Console.WriteLine("'" + _message.Text + "'已经发送!"); //producer.Send(_message2, MsgDeliveryMode.Persistent, MsgPriority.Normal, // TimeSpan.MinValue); //Console.WriteLine("'" + _message2.Text + "'已经发送!"); producer.Send(msg, MsgDeliveryMode.Persistent, MsgPriority.Normal, TimeSpan.MinValue); Console.WriteLine("你输入的数据'" + msg + "'已经发送!"); Console.WriteLine("**************************************************"); Console.BackgroundColor = ConsoleColor.Black; Console.WriteLine("请输入你要发送数据,然后回车!"); } } } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.ReadLine(); }
public static void Run(string cutomerId = "", string address = "tcp://127.0.0.1:61616/") { System.Threading.Thread t = new System.Threading.Thread(() => { try { //创建连接工厂 IConnectionFactory _factory = new ConnectionFactory(address); //创建连接 using (IConnection conn = _factory.CreateConnection()) { //设置客户端ID conn.ClientId = cutomerId; conn.Start(); //创建会话 using (ISession session = conn.CreateSession()) { //创建主题 var topic = new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("topic"); //创建消费者 IMessageConsumer consumer = session.CreateDurableConsumer(topic, "Customer", null, false); //注册监听事件 consumer.Listener += new MessageListener(consumer_Listener); //阻塞当前线程,监听消息 System.Threading.Thread.CurrentThread.Join(); } //关闭连接 conn.Stop(); conn.Close(); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); Console.WriteLine(ex.ToString()); } }); t.Start(); }
static void Main(string[] args) { try { _factory = new ConnectionFactory("tcp://127.0.0.1:61616/"); using (IConnection conn = _factory.CreateConnection()) { conn.Start(); using (ISession session = conn.CreateSession()) { var topic = new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("topic"); IMessageConsumer consumer = session.CreateDurableConsumer(topic, "Customer", null, false); consumer.Listener += new MessageListener(consumer_Listener); //没有这句话,Session会话就会被关闭 Console.Read(); } } } catch (Exception ex) { } }