public static void main(String[] args) {
RedisPlugin redisPlugin = new RedisPlugin("inpark_c", "116.62.173.151", 6379);
redisPlugin.start();
Redis.use().set("stock","20");
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
ZkDistributedLock zkDistributedLock = new ZkDistributedLock();
zkDistributedLock.setLockName("test");
if (zkDistributedLock.tryLock(ZookeeperConstant.WAIT_TIME , TimeUnit.MILLISECONDS)) {
System.out.println(new Date()+"===="+Thread.currentThread().getId()+"lock");
try {
int stock = Integer.parseInt(Redis.use().get("stock"));
if (stock > 0){
// 减库存
stock = stock -1;
Redis.use().set("stock",stock+"");
System.out.println("[扣库存成功] 剩余库存:{}"+stock);
}else {
System.out.println("[口库存或失败] 库存不足!!");
}
zkDistributedLock.unlock();
} catch (Exception e) {
e.printStackTrace();
}
}
}
},0,100,TimeUnit.MILLISECONDS);
}
消息队列
new ActiveMQPlugin("tcp://127.0.0.1:61616").start();
JmsSubjectConstantEnum order=JmsSubjectConstantEnum.ORDER;
ActiveMQ.addSender(new JmsSender(order.getSender(),ActiveMQ.getConnection(), Destination.Topic,"test"));
ActiveMQ.addReceiver(new JmsReceiver(order.getReceiver(),ActiveMQ.getConnection(), Destination.Topic, "test"));
SendUtilImpl sendUtil=new SendUtilImpl();
JSONObject msg = new JSONObject();
msg.put("test", "11");
sendUtil.sendMessage(order.getSender(),msg,10000L);
网友评论