022_Jedis的事物

1. 新建一个名为JedisTransaction的Java项目, 拷入相关jar包, 并添加到build path

2. 编辑JedisTransaction.java

package com.redis.transaction;

import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

/**
 * Jedis事物
 */
public class JedisTransaction {
	public static void main(String[] args) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				weChatPay();
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				alipay();
			}
		}).start();
	}
	
	public static void alipay() {
		try {
			Jedis jedis = new Jedis("192.168.25.138", 6379);
			jedis.auth("lyw123456");
			System.out.println("alipay start...");
			// 监听key
			jedis.watch("lock");
			// 开始事物
			Transaction transaction = jedis.multi();
			transaction.set("lock", "click game pay");
			transaction.set("lock", "create game pay order");
			transaction.set("lock", "show pay ui");
			transaction.set("lock", "choose alipay");
			transaction.set("lock", "alipay create order");
			Thread.sleep(2000);
			transaction.set("lock", "pay money");
			transaction.set("lock", "ali receive money");
			transaction.set("lock", "ali send pay call back to game server");
			transaction.set("lock", "game server receive ali pay call back");
			transaction.set("lock", "game send add coin commond to game client");
			transaction.set("lock", "game client add coin");
			// 提交事物
			List<Object> list = transaction.exec();
			for (Object object : list) {
				System.out.println("alipay: " + object);
			}
			System.out.println("alipay end...");
			jedis.close();
		} catch (Exception e) {
			System.out.println("alipay exception: " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	public static void weChatPay() {
		try {
			Jedis jedis = new Jedis("192.168.25.138", 6379);
			jedis.auth("lyw123456");
			System.out.println("wechatpay start...");
			// 监听key
			jedis.watch("lock");
			// 开始事物
			Transaction transaction = jedis.multi();
			transaction.set("lock", "click game pay");
			transaction.set("lock", "create game pay order");
			Thread.sleep(3000);
			transaction.set("lock", "show pay ui");
			transaction.set("lock", "choose wechatpay");
			transaction.set("lock", "wechatpay create order");
			transaction.set("lock", "pay money");
			transaction.set("lock", "wechat receive money");
			transaction.set("lock", "wechat send pay call back to game server");
			transaction.set("lock", "game server receive wechat pay call back");
			transaction.set("lock", "game send add coin commond to game client");
			transaction.set("lock", "game client add coin");
			// 提交事物
			List<Object> list = transaction.exec();
			for (Object object : list) {
				System.out.println("wechatpay: " + object);
			}
			System.out.println("wechatpay end...");
			jedis.close();
		} catch (Exception e) {
			System.out.println("wechatpay exception: " + e.getMessage());
			e.printStackTrace();
		}
	}
	
}

3. 运行结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值