JAVA WEB 服务端,方法如何异步执行

本文介绍了一种在Spring框架下实现方法异步执行的方式。针对移动端页面发起的长时间HTTP请求导致的超时问题,通过使用@Async注解及相应的Spring配置,将耗时操作转为异步执行,有效避免了苹果手机上的超时限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何把JAVA方法变成异步执行的方法?

    在苹果手机上移动端页面发起ajax请求到服务端,服务端对数据库增删改查之后,需要发起http请求到消息中心这样一个项目,给它加消息。

    但是http请求时间比较长,导致页面ajax请求超时了(苹果手机自带机制,超过一定时间就算超时,即使你设置了AJAX超时时间也没用)。

    如何把加消息这一步,弄成异步执行的呢?答案就在下面:

    使用spring框架的注解,@Async 加在需要异步执行的方法上,但是需要在spring的配置文件中增加配置。

1.配置文件引入必要配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="   
          http://www.springframework.org/schema/beans   
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
          http://www.springframework.org/schema/tx   
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
          http://www.springframework.org/schema/context   
          http://www.springframework.org/schema/context/spring-context-3.0.xsd   
          http://www.springframework.org/schema/aop   
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"

          http://www.springframework.org/schema/task
             http://www.springframework.org/schema/task/spring-task-4.0.xsd
">

    红色标记的是异步执行方法需要引用的内容蓝色标记的是面向切面编程引用的内容(没有使用到AOP的内容就不要引入)

      2.开启异步执行的注解

        <!-- 支持异步方法执行 -->
        <task:annotation-driven />

       3.在需要设置为异步执行的方法上加注解

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.scheduling.annotation.Async;


public class MessageAspect {
    public static Logger log = Logger.getLogger(MessageAspect.class);
     

       @Async
    public Object aroundMethod(ProceedingJoinPoint pjd, DeleteMessage deleteMessage) throws Throwable {
        Object result = null;
        Object[] param = pjd.getArgs();
        MessageClientFactory.instance();
MessageClient messageClient = MessageClientFactory.getMessageClient(deleteMessage.operation());
        messageClient.sendMessage(deleteMessage.operation(), param, result);
        result = pjd.proceed();
        return result;
    }
    
    /**
     * 方法正常结束后执行的代码 返回通知是可以访问到方法的返回值的
     */
    @Async
    public void afterReturning(JoinPoint joinPoint,Message message,Object result) {
        MESSAGEOPERATION messageoperation = message.operation();
        MessageClientFactory.instance();
 MessageClient messageClient = MessageClientFactory.getMessageClient(messageoperation);
        Object[] param = joinPoint.getArgs();
        messageClient.sendMessage(messageoperation, param, result);
    }
}

转载于:https://my.oschina.net/xiaoyoung/blog/900605

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值