Use redis to simulate mobile phone verification code sending and consumer and producer cases

It is stipulated that a mobile phone number can only request verification codes three times a day, and the verification code requested will expire in only one minute.

package com.doit.demo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Random;
import java.util.Scanner;

public class PhoneNum {
    public static void main(String[] args) {
        JedisPool pool = new JedisPool("linux01",6379);
        Jedis jedis = pool.getResource();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的手机号码:");
        String phone = sc.nextLine();
        String code = getCode();
        System.out.println("您的验证码是:"+code);
        jedis.setex("number",60,code);
        String regex = "^((13[0-9])|(14[5,6,7,9])|(15[^4])|(16[5,6])|(17[0-9])|(18[0-9])|(19[1,8,9]))\\d{8}$";
        int count =3;
        while(count>=1){
            if(jedis.get("number")==null){
                System.out.println("您的验证码已失效");
                break;
            }
            else if (phone.matches(regex)){


                String num= sc.nextLine();
                if (num.equals(jedis.get("number"))){
                    System.out.println("恭喜您,登陆成功");
                    break;
                }else{

                    count--;
                    if (count==0){
                        System.out.println("您的机会已用完");

                    }
                    System.out.println("验证码错误,您还有 "+count+" 次机会");
                }


            }else{
                System.out.println("手机号格式输入错误");
                break;
            }
        }


    }
    public static String getCode(){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int i1 = new Random().nextInt(10);
            sb.append(i1);
        }
        return sb.toString();
    }
}

Simulate task queue

Requirements: Task scheduling system: Producers continuously generate tasks and put them into task-queue.

The consumer continuously takes out tasks to process and puts them into a tmp-queue for temporary storage. If the task is successfully processed, the tmp-queue is cleared. Otherwise, the task is bounced back to the task-queue.

 

 Producer

package com.doit.demo;

import redis.clients.jedis.Jedis;

import java.util.Random;
import java.util.UUID;

public class Producer {
    public static void main(String[] args) throws InterruptedException {
        Jedis jedis = new Jedis("linux01", 6379);

        while(true){
            UUID uuid = UUID.randomUUID();
            System.out.println("任务..........."+uuid);
            jedis.lpush("task",uuid.toString());
            Thread.sleep(2000);
            if (jedis.llen("task")==10){
                Thread.sleep(6000);
            }
        }

    }
}

Consumer 

package com.doit.demo;

import redis.clients.jedis.Jedis;

import java.util.Random;

public class Consumer {
    public static void main(String[] args) throws InterruptedException {
        Jedis jedis = new Jedis("linux01", 6379);

        while(true){
            jedis.rpoplpush("task","hcq");
            int num = new Random().nextInt(10000);
            String data = jedis.rpop("hcq");
            if (num%3==0){
                System.out.println("该任务失败,重新进入缓存区...."+data);
                jedis.lpush("hcq",data);
            }
            else{
                System.out.println("读取成功"+data);
            }
            Thread.sleep(2000);
        }
    }
}

 

Guess you like

Origin blog.csdn.net/m0_53400772/article/details/130977126