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(); }
///** //* "pm_coupon_grant为分表,会员卡号作为分表键,查询需要根据卡号求出表索引,再找到对应分表查询 //* 得到hsah子表名 //* </p> //* @param splitKeyVal //* 表拆分键的值(根据splitKeyVal值来取模)<br> //* @param subTableCount //* 要拆分子表总数<br> //* @return //*/ //public static String GetSplitTableName(String tableName, Object splitKeyVal, int subTableCount) //{ // if (splitKeyVal == null) // { // throw new Exception("splitKeyVal is null.tableName:" + tableName); // } // long hashVal = splitKeyVal.ToString().GetHashCode(); // // 斐波那契(Fibonacci)散列 // hashVal = (hashVal * 2654435769L) >> 28; // // 避免hashVal超出 MAX_VALUE = 0x7fffffff时变为负数,取绝对值 // hashVal = Math.Abs(hashVal) % subTableCount; // return tableName + "_" + hashVal; //} public static void SendEmail(string[] toEmails, string mailTitle, string mailBody) { var sendHostName = "smtp.qq.com"; var userName = "******"; var pwd = "llcffwhxezsicadi"; PFDataHelper.SendEmail(userName, pwd, sendHostName, toEmails, mailTitle, mailBody, a => { a.IsBodyHtml = false; }); }
// public void BuildMqProducer(String message) { // ConnectionFactory factory = new ConnectionFactory(); // factory.setHost("localhost"); // try (Connection connection = factory.newConnection(); // Channel channel = connection.createChannel() // ) { // try { // channel.queueDeclare(_mqConfig.getQueueName(), false, false, false, null); // channel.basicPublish("", _mqConfig.getQueueName(), null, message.getBytes("UTF-8")); // String logMsg="\r\n [x][rabbitMq] queueName:"+_mqConfig.getQueueName()+" \r\n Sent '" + message + "' \r\n"; // WriteLog(logMsg); // }catch(Exception e) { // } // } catch (IOException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } catch (TimeoutException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } // } // public void BuildRocketMqProducer(String message) { //// org.apache.rocketmq.client.producer.DefaultMQProducer producer = new org.apache.rocketmq.client.producer.DefaultMQProducer("test-group"); //// producer.setNamesrvAddr("localhost:9876"); //// producer.setInstanceName("rmq-instance"); // org.apache.rocketmq.client.producer.DefaultMQProducer producer = new org.apache.rocketmq.client.producer.DefaultMQProducer(_mqConfig.getGroupId()); // producer.setNamesrvAddr(_mqConfig.getNameSrvAddr()); // if(!PFDataHelper.StringIsNullOrWhiteSpace(_mqConfig.getInstanceName())) { // producer.setInstanceName(_mqConfig.getInstanceName()); // } // try { // producer.start(); // org.apache.rocketmq.common.message.Message mmessage = new org.apache.rocketmq.common.message.Message(_mqConfig.getTopic(), _mqConfig.getTag(),message.getBytes()); // //System.out.println("生产者发送消息:"+JSON.toJSONString(user)); // org.apache.rocketmq.client.producer.SendResult sendResult= producer.send(mmessage); // if (sendResult != null) { // String logMsg="\r\n [x][rocketMq] topic:"+_mqConfig.getTopic()+" tag:"+_mqConfig.getTag()+" \r\n Sent '" + message + "' \r\n"; // WriteLog(logMsg); // } // } catch (Exception e) { // e.printStackTrace(); // } // producer.shutdown(); // } //public void BuildAliMqProducer(String message) { // //参考:D:\eclipse_workspace\IpaasTest\src\com\mq\simple\ProducerTest.java // Properties properties = GetAliMqProperties() ; // Producer producer = ONSFactory.createProducer(properties); // // 在发送消息前,必须调用 start 方法来启动 Producer,只需调用一次即可 // producer.start(); // Message msg = new Message( // // _mqConfig.getTopic(), // _mqConfig.getTag(),// "*" // message.getBytes() // ); // // 设置代表消息的业务关键属性,请尽可能全局唯一。 // // 以方便您在无法正常收到消息情况下,可通过阿里云服务器管理控制台查询消息并补发 // // 注意:不设置也不会影响消息正常收发 // String msgKey="ORDERID_" + _mqConfig.getTopic()+ PFDataHelper.ObjectToDateString(Calendar.getInstance(), "yyyyMMddHHmmss"); // msg.setKey(msgKey); // try { // SendResult sendResult = producer.send(msg); // // 同步发送消息,只要不抛异常就是成功 // if (sendResult != null) { // String logMsg="\r\n [x][aliMq] topic:"+_mqConfig.getTopic()+" tag:"+_mqConfig.getTag()+" \r\n Sent '" + message + "' \r\n"; // WriteLog(logMsg); // } // } // catch (Exception e) { // // 消息发送失败,需要进行重试处理,可重新发送这条消息或持久化这条数据进行补偿处理 // System.out.println("\r\n"+new Date() + " TIANGONG TEST -Send mq message failed. Topic is:" + msg.getTopic()+" \r\n"); // e.printStackTrace(); // } // // 在应用退出前,销毁 Producer 对象 // // 注意:如果不销毁也没有问题 // producer.shutdown(); //} //参考TestSendEmailAsync() public void BuildPFEmailMqProducer(String message) { var UserEmailUserName = PFDataHelper.SysEmailUserName; var UserEmailPwd = PFDataHelper.SysEmailPwd; var UserEmailHostName = PFDataHelper.SysEmailHostName; ////生产方(使用User邮箱,也可以用系统邮箱吧) //var rt = PFDataHelper.SendEmailAsync(UserEmailUserName, UserEmailPwd, UserEmailHostName, // new string[] { PFDataHelper.SysEmailUserName }, // _mqConfig.getTopic(), message); var rt = PFDataHelper.SendEmail(UserEmailUserName, UserEmailPwd, UserEmailHostName, new string[] { PFDataHelper.SysEmailUserName }, "PFEmailMq_product_" + _mqConfig.getTopic(), message); //rt.Wait();//先不测试回调 //var resultTitle = rt.Result.Subject; ////Assert.IsTrue(resultTitle == "PFEmailMq_consumer_" + producerEmailTitle); }