Java秒杀数字藏品并推送微信消息

Java秒杀数字藏品并推送微信消息

1.依赖

   		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.26</version>
        </dependency>
        
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.11</version>
        </dependency>

2.微信推送准备工作

关注公众号 image-20230506094147202

image-20230506094211437

image-20230506094228046

image-20230506094242958

记录下自己的token

3.编写代码

秒杀类,用于秒杀藏品,两种钱包,所以重复了两次 比较随意见谅

public class MFeng {

    public static void killProduct(String price,String id,String num){

        String  body = HttpRequest.post("https://www.mfeng.art/api/v1/nft/order/batch/market")
                .body("{\"id\":\""+id+"\",\"maxPrice\":\""+price+"\",\"qty\":\""+num+"\",\"walletId\":2}")
                .header("X-Auth-Token", "")
                .execute().body();


        String message = null;
        try {
            message = (String) JSONObject.parseObject(body)
                    .get("message");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        System.out.println("message = " + message);

        if (message.contains("操作成功")) {

            PushMessage.pushMessage("","蜜蜂抢购",id.concat(" 抢购成功 ").concat(num).concat(" 个"));

        }


        body = HttpRequest.post("https://www.mfeng.art/api/v1/nft/order/batch/market")
                .body("{\"id\":\""+id+"\",\"maxPrice\":\""+price+"\",\"qty\":\""+num+"\",\"walletId\":1}")
                .header("X-Auth-Token", "")
                .execute().body();


        try {
            message = (String) JSONObject.parseObject(body)
                    .get("message");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        System.out.println("message = " + message);

        if (message.contains("操作成功")) {

            PushMessage.pushMessage("1eb175de899047369c8d42dd0c7529ef","蜜蜂抢购",id.concat(" 抢购成功 ").concat(num).concat(" 个"));

        }
    }

}

推送类,用于抢购后推送微信消息

//推送消息到微信
public class PushMessage {


    public static String pushMessage(String token, String title, String message) {

        HashMap<String, String> bodyMap = new HashMap<>();

        if (!title.isEmpty()) {
            bodyMap.put("title", title);
        }

        bodyMap.put("content", message);

        String body = HttpRequest.post("http://www.pushplus.plus/send/" + token)
                .body(JSON.toJSONString(bodyMap))
                .execute().body();

        return body;

    }

}

4.效果

最终当抢购成功后,就会及时发送消息推送给个人微信

image-20230506094536160

实现数字藏品抢购功能在Java中需要考虑高发和线程安全的问题。以下是一个基本的实现思路: 1. **据库设计**: - 创建一个表来存储数字藏品的信息,包括藏品ID、名称、量等。 - 创建一个表来存储用户抢购的记录,包括用户ID、藏品ID、抢购时间等。 2. **后端逻辑**: - 使用Spring Boot框架来快速搭建后端服务。 - 使用Redis来缓存库存信息,提高读取速度。 - 使用据库事务来确保据的一致性。 3. **高发处理**: - 使用乐观锁或悲观锁来防止超卖。 - 使用消息队列(如RabbitMQ)来异步处理抢购请求。 4. **前端界面**: - 创建一个简单的HTML页面,用户可以点击抢购按钮。 - 使用AJAX请求后端接口进行抢购。 以下是一个简单的示例代码: ```java @RestController @RequestMapping("/api/digital-collectibles") public class DigitalCollectibleController { @Autowired private DigitalCollectibleService digitalCollectibleService; @PostMapping("/抢购") public ResponseEntity<String>抢购(@RequestParam("userId") Long userId, @RequestParam("collectibleId") Long collectibleId) { try { boolean success = digitalCollectibleService.purchase(userId, collectibleId); if (success) { return ResponseEntity.ok("抢购成功"); } else { return ResponseEntity.ok("抢购失败,库存不足"); } } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器错误"); } } } @Service public class DigitalCollectibleService { @Autowired private DigitalCollectibleRepository digitalCollectibleRepository; @Autowired private RedisTemplate<String, Object> redisTemplate; public boolean purchase(Long userId, Long collectibleId) { String key = "collectible:" + collectibleId; Long count = redisTemplate.opsForValue().increment(key, -1); if (count >= 0) { // 异步保存抢购记录 digitalCollectibleRepository.purchase(userId, collectibleId); return true; } else { redisTemplate.opsForValue().increment(key, 1); return false; } } } @Repository public interface DigitalCollectibleRepository { @Modifying @Transactional @Query("INSERT INTO purchase_records (user_id, collectible_id, purchase_time) VALUES (:userId, :collectibleId, NOW())") void purchase(@Param("userId") Long userId, @Param("collectibleId") Long collectibleId); } ``` 这个示例代码展示了如何使用Spring Boot和Redis来实现数字藏品抢购功能。为了提高系统的发处理能力,可以进一步优化据库索引、使用分布式锁等技术。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋日的晚霞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值