企业做网站报价,试析媒体网站品牌建设,满足客户的分销管理系统,做seo推广做网站有用吗在我们系统中有一个 订单催付 的场景#xff0c;我们的客户在天猫下的订单 , 淘宝会及时将订单推送给我们#xff0c;如果在用户设定的时间内未付款那么就会给用户推送一条短信提醒#xff0c;很简单的一个功能对吧#xff0c;但是#xff0c;tianmao商家对我们来说#… 在我们系统中有一个 订单催付 的场景我们的客户在天猫下的订单 , 淘宝会及时将订单推送给我们如果在用户设定的时间内未付款那么就会给用户推送一条短信提醒很简单的一个功能对吧但是tianmao商家对我们来说肯定是要分大客户和小客户的对吧比如像苹果小米这样大商家一年起码能给我们创造很大的利润所以理应当然他们的订单必须得到优先处理而曾经我们的后端系统是使用 redis 来存放的定时轮询大家都知道 redis 只能用 List 做一个简简单单的消息队列并不能实现一个优先级的场景 所以订单量大了后采用 RabbitMQ 进行改造和优化 , 如果发现是大客户的订单给一个相对比较高的优先级否则就是默认优先级。 添加队列
控制台添加 代码添加优先级
MapString, Object params new HashMap();
params.put(x-max-priority, 10);
channel.queueDeclare(hello, true, false, false, params); 消息中代码添加优先级
AMQP.BasicProperties properties new AMQP.BasicProperties().builder().priority(5).build(); 注意事项 要让队列实现优先级需要做的事情有如下事情 : 队列需要设置为优先级队列消息需要设置消息的优先级消费者需要等待消息已经发送到队列中才去消费因为这样才有机会对消息进行排序 代码
工具类
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;public class RabbitMqUtils {//得到一个连接的 channelpublic static Channel getChannel() throws Exception{//创建一个连接工厂ConnectionFactory factory new ConnectionFactory();factory.setHost(43.139.59.28);factory.setUsername(guest);factory.setPassword(guest);Connection connection factory.newConnection();Channel channel connection.createChannel();return channel;}
}
生产者
import com.example.demo.util.RabbitMqUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;public class Producer {private static final String QUEUE_NAMEhello;public static void main(String[] args) throws Exception {try (Channel channel RabbitMqUtils.getChannel();) {//给消息赋予一个 priority 属性AMQP.BasicProperties properties new AMQP.BasicProperties().builder().priority(5).build();for (int i 1; i 11; i) {String message infoi;if(i5){channel.basicPublish(, QUEUE_NAME, properties, message.getBytes());}else{channel.basicPublish(, QUEUE_NAME, null, message.getBytes());}System.out.println(发送消息完成: message);}}}
} 生产者
import com.example.demo.util.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;import java.util.HashMap;
import java.util.Map;public class Consumer {private static final String QUEUE_NAMEhello;public static void main(String[] args) throws Exception {Channel channel RabbitMqUtils.getChannel();//设置队列的最大优先级 最大可以设置到 255 官网推荐 1-10 如果设置太高比较吃内存和 CPUMapString, Object params new HashMap();params.put(x-max-priority, 10);channel.queueDeclare(QUEUE_NAME, true, false, false, params);System.out.println(消费者启动等待消费......);DeliverCallback deliverCallback(consumerTag, delivery)-{String receivedMessage new String(delivery.getBody());System.out.println(接收到消息:receivedMessage);};channel.basicConsume(QUEUE_NAME,true,deliverCallback,(consumerTag)-{System.out.println(消费者无法消费消息时调用如队列被删除);});}
} 结果 i5的消息被设置优先级为5所以消息被最先消费 文章转载自: http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.rnds.cn.gov.cn.rnds.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.mpflb.cn.gov.cn.mpflb.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.jnrry.cn.gov.cn.jnrry.cn http://www.morning.trzzm.cn.gov.cn.trzzm.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.yngtl.cn.gov.cn.yngtl.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn http://www.morning.kbdrq.cn.gov.cn.kbdrq.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.nkyqh.cn.gov.cn.nkyqh.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.hctgn.cn.gov.cn.hctgn.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.shuangxizhongxin.cn.gov.cn.shuangxizhongxin.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.zlfxp.cn.gov.cn.zlfxp.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.gwwtm.cn.gov.cn.gwwtm.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn http://www.morning.crfyr.cn.gov.cn.crfyr.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.znnsk.cn.gov.cn.znnsk.cn http://www.morning.qqhersx.com.gov.cn.qqhersx.com http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.xyrw.cn.gov.cn.xyrw.cn http://www.morning.qgqck.cn.gov.cn.qgqck.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.nzfqw.cn.gov.cn.nzfqw.cn http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.yrkdq.cn.gov.cn.yrkdq.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.yfrlk.cn.gov.cn.yfrlk.cn http://www.morning.phnbd.cn.gov.cn.phnbd.cn http://www.morning.nkjnr.cn.gov.cn.nkjnr.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn