增加图片阻塞队列缓冲,当图片减少被使用时候,生产者自动生产保证10个图片缓存。
package com.example.demo.ctrl;
import com.example.demo.util.VerificationImageGenerator;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
@RequestMapping("/verify")
@Controller
public class VerifyCodeCtrl {
private static Logger logger = Logger.getLogger(VerifyCodeCtrl.class);
private static ArrayBlockingQueue<CustomImage> bufferedImages = new ArrayBlockingQueue<>(10);
private ExecutorService executorService= Executors.newSingleThreadExecutor();
@PostConstruct
private void initBufferedImages() {
executorService.submit(new Producer());
}
private static class Producer implements Runnable{
@Override
public void run() {
product();
}
private void product(){
while (true){
try {
bufferedImages.put(generateBufferedImage());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@RequestMapping(value = "/code",method = RequestMethod.GET)
public void verifycode(@RequestParam("uid") String uid, HttpServletResponse response, HttpSession session) {
BufferedImage image = generateRandCode(uid);
writeToResponse(response, image);
}
private void writeToResponse(HttpServletResponse response, BufferedImage image) {
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 240);
ServletOutputStream out = null;
try {
out = response.getOutputStream();
ImageIO.write(image, "JPEG", out);
}catch (IOException e) {
logger.error("[VerifyCodeController.verifycode]: " + e);
}finally {
IOUtils.closeQuietly(out);
}
}
private BufferedImage generateRandCode(String uid) {
CustomImage customImage = bufferedImages.poll();
if (customImage == null) {
customImage = generateBufferedImage();
}
//save uid and code relation.
return customImage.bufferedImage;
}
private static CustomImage generateBufferedImage() {
CustomImage customImage = new CustomImage();
String identifying= randomNumberStr(6);
customImage.verifyCode = identifying;
customImage.bufferedImage = new VerificationImageGenerator(new Random()).bufferedImage(identifying);
return customImage;
}
public static String randomNumberStr(int length) {
java.util.List<Integer> list = new ArrayList<>();
for (int i = 0; i < length; i++) {
list.add(new Random().nextInt(10));
}
Collections.shuffle(list);
StringBuilder sb = new StringBuilder();
list.forEach(sb::append);
return sb.toString();
}
private static class CustomImage {
public String verifyCode;
public BufferedImage bufferedImage;
}
}
图片生成类
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class VerificationImageGenerator {
private static final int WIDTH = 70;
private static final int HEIGHT = 35;
private static final Color IMG_BACK_1 = new Color(165, 42, 42);
private static final Color IMG_BACK_2 = new Color(238, 18, 137);
private static final Color IMG_BACK_3 = new Color(100, 149, 237);
private static final Color IMG_BACK_4 = new Color(238, 238, 0);
private static final int FONT_SIZE = 25;
private final Random random;
public VerificationImageGenerator(Random random) {
this.random = random;
}
public BufferedImage bufferedImage(String identifying) {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setFont(new Font("Times New Roman", Font.BOLD, FONT_SIZE));
for(int i = 0; i < identifying.length(); i++){
if(i+1==1){
g.setColor(IMG_BACK_1);
}else if(i+1==2){
g.setColor(IMG_BACK_2);
}else if(i+1==3){
g.setColor(IMG_BACK_3);
}else if(i+1==4){
g.setColor(IMG_BACK_4);
}
g.drawString(String.valueOf(identifying.charAt(i)), 13 * i + 6, 25);
}
//g.drawOval(0, 12, 60, 11);
for (int i = 0; i < 100; i++) {
int x = random.nextInt(WIDTH);
int y = random.nextInt(HEIGHT);
g.drawLine(x, y, x, y);
}
g.dispose();
return image;
}
}
测试环境:
64位,win10系统笔记本
CPU:i5-4210U @1.7GHz 2.39GHz
可用RAM:6G
测试结果:
Time Total:0.996687 sec
Time Fastest:0.048034 sec
Time Slowest:0.915648 sec
Success Num:300
Fail Num:0
Response Size:490446 bytes
Requests per second:300.997083 [#/sec]
temporal distribution:[percent %] [time min sec]~[time max sec]
10% 0.048034~0.338240
20% 0.338240~0.416295
30% 0.416295~0.433307
40% 0.433307~0.463329
50% 0.464330~0.741526
60% 0.741526~0.817580
70% 0.820582~0.880625
80% 0.881625~0.891632
90% 0.891633~0.901639
100% 0.901639~0.915648