Spring demo example using a thread pool

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/sgl520lxl/article/details/89361357

A: xml configuration file which is configured to generate bean

<bean id="taskExecutor"
		class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
		<property name="corePoolSize" value="5" />
		<property name="maxPoolSize" value="10" />
		<property name="queueCapacity" value="25" />
		<property name="keepAliveSeconds" value="300" />
</bean>	

Springframework is used here in the thread pool to manage instances org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor

Projects need to import relevant jar package

 

II: injection thread pool object you need to use the thread pool class inside

@Autowired
@Qualifier("taskExecutor")
private TaskExecutor taskExecutor;

 

In the service which can start a thread from the thread pool to perform operations directly inside

        //5,IOS推送通道
        AniuMessage newMsg = new AniuMessage();
        newMsg.setMessageChannelType(AniuMessage.MESSAGE_CHANNELTYPE_IOS);
        AniuPushMessageListRunnable pushApp = new   AniuPushMessageListRunnable(newMsg,aniuMessageService);
        taskExecutor.execute(pushApp);

Here AniuPushMessageListRunnable Runnable class is the actual implementation and the business logic class

newMsg examples of various types of parameters are stored for easy AniuPushMessageListRunnable to deal with related business

aniuMessageService service service is a logical operation for the inside of the injection, as a parameter passed AniuPushMessageListRunnable class, which facilitate AniuPushMessageListRunnable aniuMessageService directly related to process business logic

 

AniuPushMessageListRunnable logical processing thread written classes Example:

/**
 *  对指定列表用户推送消息
 *  
 * @author administrator
 * @date 2019年4月4日
 */
public class AniuPushMessageListRunnable implements Runnable{

	private static final Logger LOGGER = Logger.getLogger(AniuPushMessageListRunnable.class);

	private AniuMessageService aniuMessageService;
	
	private AniuMessage aniuMessage;


	
	public AniuPushMessageListRunnable(AniuMessage aniuMessage,AniuMessageService aniuMessageService) {
		super();
		this.aniuMessageService = aniuMessageService;
		this.aniuMessage = aniuMessage;
	}


	
	@Override
	public void run() {
		
		LOGGER.info("--------------------开始[对指定列表用户推送消息]线程已启动");
		if(aniuMessage == null){
			LOGGER.info("--------------------推送的消息对象是空");
		}else{
			//这里是处理相关业务逻辑,可以用传入的aniuMessageService 去处理相关逻辑
			//如果需要更多的service,可以拓展此类的构造函数,增加形参,传入多个service
		}
		
		LOGGER.info("--------------------结束[对指定列表用户推送消息]线程已启动");
	}
}

 

 

Guess you like

Origin blog.csdn.net/sgl520lxl/article/details/89361357