MQ 简单 异步客户端

 1 public void InitializeMQClient()
 2         {
 3 
 4             try
 5             {
 6                 const string hostName = "hostName";
 7                 const string channelName = "SYSTEM.DEF.SVRCONN";
 8                 const string topicName = "appSecurityPush";
 9 
10                 XMSFactoryFactory xff = XMSFactoryFactory.GetInstance( XMSC.CT_WMQ );
11                 IConnectionFactory cf = xff.CreateConnectionFactory( );
12                 cf.SetStringProperty( XMSC.WMQ_HOST_NAME, hostName );
13                 cf.SetIntProperty( XMSC.WMQ_PORT, 1414 );
14                 cf.SetStringProperty( XMSC.WMQ_CHANNEL, channelName );
15 
16                 IConnection conn = cf.CreateConnection( );
17                 ISession sess = conn.CreateSession( false, AcknowledgeMode.AutoAcknowledge );
18                 IDestination topic = sess.CreateTopic( "topic://" + topicName );
19                 IMessageConsumer consumer = sess.CreateConsumer( topic );
20                 consumer.MessageListener = OnMessage;
21                 conn.Start( );
22             }
23             catch
24             {
25             }
26         }
27 
28         public void OnMessage(IMessage message)
29         {
30             if ( message != null )
31             {
32                 try
33                 {
34                     var textMsg = ( ITextMessage ) message;
35                     if ( !string.IsNullOrEmpty( textMsg.Text ) )
36                     {
37                         //TODO something
38                     }
39                 }
40                 catch
41                 {
42                 }
43             }
44         }

 

posted @ 2013-10-31 21:03  DavisPing  阅读(565)  评论(0编辑  收藏  举报