java web 异步调用_JAVA WEB 服务端,方法如何异步执行

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

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

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

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

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

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

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.开启异步执行的注解

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值