public void BuildPFEmailMqConsumer(PFMqHelper.PFConsumerResponseTask pfDeliverCallback) { PFMqConfig mqConfig = pfDeliverCallback.GetMqConfig(_mqConfig); //string producerEmailTitle = "PFEmailMq_producer_" + "会员资料表";//中文有问题--benjamin todo //string producerEmailTitle = "PFEmailMq_producer_" + "hyzl"; //消费方(使用系统邮箱) string result = ""; //bool success = false; var consumerTask = new PFListenEmailTask("PFEmailMqConsumerListener_" + mqConfig.getTopic(), new PFEmailManager(PFDataHelper.SysEmailHostName, PFDataHelper.SysEmailUserName, PFDataHelper.SysEmailPwd), email => { //result = "{success:true}"; PFMqMessage pfMessage = new PFMqMessage(email); var r = pfDeliverCallback.handle(mqConfig.getTag(), pfMessage); if (r != null) { var UserEmailUserName = PFDataHelper.SysEmailUserName; ////消费方回复邮件(暂不回复--benjamin) PFDataHelper.SendEmail(PFDataHelper.SysEmailUserName, PFDataHelper.SysEmailPwd, PFDataHelper.SysEmailHostName, new string[] { UserEmailUserName }, "PFEmailMq_consumer_Response_" + mqConfig.getTopic() + email.Body, JsonConvert.SerializeObject(r)); } }, (email//, task ) => { //消费方监听生产方邮件 //return email.Subject != null && email.Subject.IndexOf("TestForceUpdateHyzl_") == 0;//这里不要用>-1,否则可能把自动回复的邮件也当作是了 return(email.Subject == "PFEmailMq_product_" + mqConfig.getTopic()); }); consumerTask.Start(); }
// public void BuildRabbitMqConsumer(PFMqHelper.PFConsumerTask pfDeliverCallback) { // try { // //rabbitmq // PFMqConfig mqConfig=pfDeliverCallback.GetMqConfig(_mqConfig); // com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory(); // factory.setHost(mqConfig.getHost()); // Connection connection; // connection = factory.newConnection(); // Channel channel = connection.createChannel(); // String QUEUE_NAME=mqConfig.getQueueName(); // channel.queueDeclare(QUEUE_NAME, false, false, false, null); // System.out.println("\r\n [*][rabbitMq] queueName:"+QUEUE_NAME+"\r\n Waiting for messages.\r\n"); // DeliverCallback deliverCallback = (consumerTag, delivery) -> { // PFMqMessage pfMessage=new PFMqMessage(delivery); // String logMsg="\r\n [x][rabbitMq] queueName:"+QUEUE_NAME+"\r\n "+pfDeliverCallback.getClass().getSimpleName()+" Received '" + pfMessage.getMessage() + "' \r\n"; // WriteLog(logMsg); // pfDeliverCallback.handle(consumerTag,pfMessage); // }; // channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { }); // } catch (IOException | TimeoutException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // public void BuildRocketMqConsumer(PFConsumerTask pfDeliverCallback) { // try { // PFMqConfig mqConfig=pfDeliverCallback.GetMqConfig(_mqConfig); // //org.apache.rocketmq.client.consumer.DefaultMQPushConsumer consumer = new org.apache.rocketmq.client.consumer.DefaultMQPushConsumer("test-group"); // // consumer.setNamesrvAddr("localhost:9876"); // // consumer.setInstanceName("rmq-instance"); // // consumer.subscribe("log-topic", "user-tag"); // org.apache.rocketmq.client.consumer.DefaultMQPushConsumer consumer = new org.apache.rocketmq.client.consumer.DefaultMQPushConsumer(mqConfig.getGroupId()); // consumer.setNamesrvAddr(mqConfig.getNameSrvAddr()); // if(!PFDataHelper.StringIsNullOrWhiteSpace(_mqConfig.getInstanceName())) { // consumer.setInstanceName(_mqConfig.getInstanceName()); // } // consumer.subscribe(mqConfig.getTopic(),mqConfig.getTag()); // consumer.registerMessageListener(new org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently() { // @Override // public org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { // for (MessageExt msg : msgs) { // PFMqMessage pfMessage=new PFMqMessage(msg); // String logMsg="\r\n [x][rocketMq] topic:"+mqConfig.getTopic()+" tag:"+mqConfig.getTag()+"\r\n "+pfDeliverCallback.getClass().getSimpleName()+" Received '" + pfMessage.getMessage() + "' \r\n "; // WriteLog(logMsg); // pfDeliverCallback.handle( mqConfig.getTag(),pfMessage); // } // return org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus.CONSUME_SUCCESS; // } // }); //// consumer.registerMessageListener(new org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly() { //// //// @Override //// public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { //// for (MessageExt msg : msgs) { //// System.out.println("消费者消费数据:"+new String(msg.getBody())); //// } //// return org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus.SUCCESS; //// } //// }); // consumer.start(); // System.out.println("\r\n [*][rocketMq] topic:"+mqConfig.getTopic()+" tag:"+mqConfig.getTag()+"\r\n Waiting for messages.\r\n"); // } catch (MQClientException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // public void BuildAliMqConsumer(PFConsumerTask pfDeliverCallback) { // //参考:D:\eclipse_workspace\IpaasTest\src\com\mq\simple\ConsumerTest.java // Properties properties = GetAliMqProperties(); // PFMqConfig mqConfig=pfDeliverCallback.GetMqConfig(_mqConfig); // Consumer consumer = ONSFactory.createConsumer(properties); // consumer.subscribe(mqConfig.getTopic(), mqConfig.getTag(), new MessageListener() { // public Action consume(Message message, ConsumeContext context) { // PFMqMessage pfMessage=new PFMqMessage(message); // String logMsg="\r\n [x][aliMq] topic:"+mqConfig.getTopic()+" tag:"+mqConfig.getTag()+"\r\n "+pfDeliverCallback.getClass().getSimpleName()+" Received '" + pfMessage.getMessage() + "' \r\n "; // WriteLog(logMsg); // pfDeliverCallback.handle( mqConfig.getTag(),pfMessage); // return Action.CommitMessage; // } // }); // consumer.start(); // System.out.println("\r\n [*][aliMq] topic:"+mqConfig.getTopic()+" tag:"+mqConfig.getTag()+"\r\n Waiting for messages.\r\n"); // } //public void BuildPFEmailMqConsumer(PFMqHelper.PFConsumerTask pfDeliverCallback) //{ // PFMqConfig mqConfig = pfDeliverCallback.GetMqConfig(_mqConfig); // //string producerEmailTitle = "PFEmailMq_producer_" + "会员资料表"; // //string producerEmailTitle = "PFEmailMq_producer_" + "hyzl"; // //消费方(使用系统邮箱) // string result = ""; // //bool success = false; // var consumerTask = new PFListenEmailTask("PFEmailMqConsumerListener_" + mqConfig.getTopic(), // new PFEmailManager(PFDataHelper.SysEmailHostName, PFDataHelper.SysEmailUserName, PFDataHelper.SysEmailPwd), // email => // { // //result = "{success:true}"; // PFMqMessage pfMessage = new PFMqMessage(email); // var r = pfDeliverCallback.handle(mqConfig.getTag(), pfMessage); // if (r != null) // { // var UserEmailUserName = PFDataHelper.SysEmailUserName; // ////消费方回复邮件(暂不回复--benjamin) // PFDataHelper.SendEmail(PFDataHelper.SysEmailUserName, PFDataHelper.SysEmailPwd, PFDataHelper.SysEmailHostName, // new string[] { UserEmailUserName }, "PFEmailMq_consumer_Response_" + email.Subject + email.Body, // JsonConvert.SerializeObject(r)); // } // }, // (email//, task // ) => // { // //消费方监听生产方邮件 // //return email.Subject != null && email.Subject.IndexOf("TestForceUpdateHyzl_") == 0;//这里不要用>-1,否则可能把自动回复的邮件也当作是了 // return email.Subject == mqConfig.getTopic(); // }); // consumerTask.Start(); //} public void BuildPFEmailMqConsumer(PFMqHelper.PFConsumerTask pfDeliverCallback) { PFMqConfig mqConfig = pfDeliverCallback.GetMqConfig(_mqConfig); //string producerEmailTitle = "PFEmailMq_producer_" + "会员资料表"; //string producerEmailTitle = "PFEmailMq_producer_" + "hyzl"; //消费方(使用系统邮箱) string result = ""; //bool success = false; var consumerTask = new PFListenEmailTask("PFEmailMqConsumerListener_" + mqConfig.getTopic(), new PFEmailManager(PFDataHelper.SysEmailHostName, PFDataHelper.SysEmailUserName, PFDataHelper.SysEmailPwd), email => { //result = "{success:true}"; PFMqMessage pfMessage = new PFMqMessage(email); pfDeliverCallback.handle(mqConfig.getTag(), pfMessage); }, (email//, task ) => { //消费方监听生产方邮件 //return email.Subject != null && email.Subject.IndexOf("TestForceUpdateHyzl_") == 0;//这里不要用>-1,否则可能把自动回复的邮件也当作是了 return(email.Subject == "PFEmailMq_product_" + mqConfig.getTopic()); }); consumerTask.Start(); }
/// <summary> /// 异步是要等待消费者的回复 /// </summary> /// <param name="message"></param> /// <returns></returns> public Task <PFEmail> BuildPFEmailMqProducerAsync(String message) { var rt = new Task <PFEmail>(() => { var UserEmailUserName = PFDataHelper.SysEmailUserName; var UserEmailPwd = PFDataHelper.SysEmailPwd; var UserEmailHostName = PFDataHelper.SysEmailHostName; //生产方监听回复 PFEmail result = null; bool hasGotResult = false; var st = DateTime.Now; //var nowStr = DateTime.Now.ToString(PFDataHelper.DateFormat); var producerListenTask = new PFListenEmailTask("PFEmailMqProducerListener_" + _mqConfig.getTopic(), new PFEmailManager(UserEmailHostName, UserEmailUserName, UserEmailPwd), email => { result = email; hasGotResult = true; }, (email//, task ) => { return(email.Subject == "PFEmailMq_consumer_Response_" + _mqConfig.getTopic() + message); }, true); producerListenTask.Start(); //Thread.Sleep(2000);//不延迟的话,后面太快了,前面还没开始监听(其实没问题,因为_lastListenTime是在PFListenEmailTask初始化时就赋值了 ////生产方发邮件 BuildPFEmailMqProducer(message); //PFDataHelper.SendEmail(UserEmailUserName, UserEmailPwd, UserEmailHostName, // new string[] { PFDataHelper.SysEmailUserName }, // _mqConfig.getTopic(), message); while (!hasGotResult) { if ((DateTime.Now - st).TotalHours > 1) { producerListenTask.Dispose(); result = new PFEmail(); result.Subject = "消费者超过1小时没有响应"; break; } Thread.Sleep(2000); } producerListenTask.NaturalStop(); return(result); }); rt.Start(); return(rt); }