当前位置: 首页 > news >正文

做网站推广的销售电话开场白沧州 中企动力提供网站建设

做网站推广的销售电话开场白,沧州 中企动力提供网站建设,wordpress如何爬虫,晋城推广型网站建设创建Spring Boot项目 首先,你需要创建一个Spring Boot项目。你可以使用Spring Initializr(https://start.spring.io/)来快速生成项目结构。 添加异步支持依赖 在你的pom.xml文件中,确保你已经添加了Spring Boot的starter依赖&…
  1. 创建Spring Boot项目
    首先,你需要创建一个Spring Boot项目。你可以使用Spring Initializr(https://start.spring.io/)来快速生成项目结构。

  2. 添加异步支持依赖
    在你的pom.xml文件中,确保你已经添加了Spring Boot的starter依赖,特别是spring-boot-starter-web,这将提供异步支持所需的依赖。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 配置异步线程池参数
    在Spring Boot中,你可以通过application.properties或application.yml文件来配置异步线程池的参数。以下是一个使用application.properties文件的示例:
# 异步线程池配置
async.executor.thread.core_pool_size=5
async.executor.thread.max_pool_size=10
async.executor.thread.queue_capacity=25
async.executor.thread.name.prefix=async-
async.executor.thread.keep_alive_seconds=60
async.executor.thread.await_termination_seconds=60
  1. 编写配置类以定义线程池
    接下来,你需要编写一个配置类,使用@Configuration注解来定义线程池。在这个配置类中,你将使用@Bean注解来创建一个ThreadPoolTaskExecutor实例,并根据application.properties中的配置来设置线程池的参数。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;@Configuration
@EnableAsync
public class AsyncConfig {@Value("${async.executor.thread.core_pool_size}")private int corePoolSize;@Value("${async.executor.thread.max_pool_size}")private int maxPoolSize;@Value("${async.executor.thread.queue_capacity}")private int queueCapacity;@Value("${async.executor.thread.name.prefix}")private String namePrefix;@Value("${async.executor.thread.keep_alive_seconds}")private int keepAliveSeconds;@Value("${async.executor.thread.await_termination_seconds}")private int awaitTerminationSeconds;@Bean(name = "taskExecutor")public Executor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(corePoolSize);executor.setMaxPoolSize(maxPoolSize);executor.setQueueCapacity(queueCapacity);executor.setThreadNamePrefix(namePrefix);executor.setKeepAliveSeconds(keepAliveSeconds);executor.setAwaitTerminationSeconds(awaitTerminationSeconds);executor.setWaitForTasksToCompleteOnShutdown(true);executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());executor.initialize();return executor;}
}
  1. 编写异步任务类
    现在,你可以编写一个包含异步方法的类。在这个类中,你将使用@Async注解来标记需要异步执行的方法,并指定前面定义的线程池(在这个例子中是taskExecutor)。
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Service
public class AsyncService {// 在需要使用异步处理的方法上添加注解@Async("taskExecutor")public void executeAsyncTask() {System.out.println("执行异步任务 - " + Thread.currentThread().getName());try {// 模拟长时间运行的任务Thread.sleep(5000);} catch (InterruptedException e) {Thread.currentThread().interrupt();}}
}
  1. 最后,在你的控制器或其他服务类中,你可以调用这个异步方法来执行异步任务。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class AsyncController {@Autowiredprivate AsyncService asyncService;@GetMapping("/async")public String handleAsyncRequest() {asyncService.executeAsyncTask();return "异步任务已启动";}
}

通过以上步骤,你就成功地在Spring Boot中配置了异步线程池,并编写了一个异步任务类来演示如何使用它。这样,你就可以在需要时轻松地将任务交给线程池异步执行了。

注意:
@Async注解不生效
‌从Spring容器管理的bean中调用‌:@Async注解的方法必须是从Spring容器管理的bean中调用的。如果从一个非Spring管理的类中调用,那么异步效果将不会生效。
‌自调用问题‌:同一个类中的方法调用不会被Spring的AOP代理捕获,因此如果在一个类中调用同一个类的另一个@Async方法,那么异步效果也不会生效。为了避免这个问题,可以将异步方法移动到另一个bean中,并通过依赖注入来调用。

http://www.yayakq.cn/news/856178/

相关文章:

  • 如何快速备案网站最新国家大事新闻
  • 建设网站投资多少wordpress怎么修改数据库密码
  • 比较有名的设计网站注册一家公司
  • 自己录入数据做问卷的网站网站建设流程是什么意思
  • 深圳开发网站建设设计网站推广的广告语
  • 网站首页收录突然没有了网站制作对公司的作用
  • 网站访问加速器揭阳网站制作工具
  • 闸北区网站建设网怎么做网站的浏览量统计
  • 个人建设网站制作关注公众号推广2元一个
  • 淄博网站建设相关文章东莞网站建设网站
  • thinkphpcmf网站开发烟台快速建站有哪些公司
  • 请专业公司做个网站要花多少钱nginx wordpress 固定链接
  • 教育学校网站做铁岭做网站的
  • 那个网站可以做数学题赚钱订票网站开发公司
  • 建设厅五大员证书查询网站iis 网站没有上传权限
  • 修改网站图片网站客户留言
  • 营销型网站建设对比分析北京兄弟搬家公司
  • 阿里云主机建网站开发信息系统软件有哪些
  • 接做室内效果图的网站网站推广优化外包
  • 深圳做网站的网络公司应用商店安全下载
  • 塘沽手机网站建设2020最成功的网络营销
  • 建设咨询网站不用代码做网站的工具
  • 关键词首页排名代发呼和浩特网站优化公司
  • 减肥产品网站模板怎样创造网站
  • 网站如何进行内外营销运营个人备案后可以做电影网站吗
  • 泉州网站建设电话设置网站的默认页面
  • dede网站seo泉州网站建设哪家公司好
  • 网站域名怎么设置重庆市工程造价信息网查询
  • 中国购物网站设计欣赏学网站建设可以从事什么工作
  • 网站留言如何做的后期网站