UIL020010.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using CoreFS.CA06;
  12. using Core.Sip.Amq.client;
  13. using Core.Sip.Amq.transport;
  14. namespace Core.LZMes.Client.UIL.UIL02
  15. {
  16. public partial class UIL020010 : FrmBase, IMessageListener
  17. {
  18. // 连接消息系统
  19. Core.Sip.Amq.client.Client _connection;
  20. IClientSession _session;
  21. RangeSet _range;
  22. public UIL020010()
  23. {
  24. InitializeComponent();
  25. string host = "localhost";
  26. int port = 5672;
  27. string virtualhost = "test";
  28. string username = "guest";
  29. string password = "guest";
  30. _range = new RangeSet();
  31. _connection = new Core.Sip.Amq.client.Client();
  32. try
  33. {
  34. _connection.Connect(host, port, virtualhost, username, password);
  35. _session = _connection.CreateSession(50000);
  36. // 队列声明
  37. }
  38. catch (Exception e)
  39. {
  40. MessageBox.Show("连接错误:" + e.Message);
  41. }
  42. }
  43. private void initAmq()
  44. {
  45. _session.QueueDeclare("MQ_SIP_AMF", Option.EXCLUSIVE, Option.AUTO_DELETE);
  46. _session.ExchangeBind("MQ_SIP_AMF", "amq.direct", "RK_SIP_MQ");
  47. _session.AttachMessageListener(this, "MQ_SIP_AMF");
  48. }
  49. // 消息接收接口
  50. public void MessageTransfer(IMessage m)
  51. {
  52. BinaryReader reader = new BinaryReader(m.Body, Encoding.UTF8);
  53. byte[] body = new byte[m.Body.Length - m.Body.Position];
  54. reader.Read(body, 0, body.Length);
  55. ASCIIEncoding enc = new ASCIIEncoding();
  56. string message = enc.GetString(body);
  57. Console.WriteLine("Message: " + message);
  58. string routingKey = m.DeliveryProperties.GetRoutingKey();
  59. // Add this message to the list of message to be acknowledged
  60. _range.Add(m.Id);
  61. if (message.Equals("That's all, folks!"))
  62. {
  63. // Acknowledge all the received messages
  64. _session.MessageAccept(_range);
  65. lock (_session)
  66. {
  67. Monitor.Pulse(_session);
  68. }
  69. }
  70. }
  71. private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
  72. {
  73. }
  74. }
  75. }