gpt4 book ai didi

java - 我可以在 Grails 中使用 @Async 注释吗

转载 作者:行者123 更新时间:2023-12-02 00:41:57 26 4
gpt4 key购买 nike

根据以下 spring 文档 link我可以使用 @Async 注释来使方法调用异步。我可以从我拥有的 java src 文件在 Grails 中使用此功能吗?

[更新]这是我的 java(netty) 套接字处理程序类,它接收套接字数据包。
公共(public)类 DefaultHandler 扩展 SimpleChannelUpstreamHandler {

private static final Logger LOG = LoggerFactory.getLogger(DefaultHandler.class);

private AggregateSocketData aggregateSocketData;

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
LOG.trace("In messageRecieved method with event: {}",e);
IEvent event = Events.dataInEvent(e.getMessage());
System.out.println(Thread.currentThread().getName());
aggregateSocketData.receiveSocketData(event);
}


@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
LOG.error("Exception occurred in Default Handler: " ,e.getCause());
}

public AggregateSocketData getAggregateSocketData() {
return aggregateSocketData;
}

public void setAggregateSocketData(AggregateSocketData aggregateSocketData) {
this.aggregateSocketData = aggregateSocketData;
}

}

我已将其作为 grails-conf-resources.xml 中的 bean

<bean id="defaultECM1240Handler" class="com.appcapture.buildingmgr.netty.DefaultHandler"
scope="prototype">
<property name="aggregateSocketData" ref="binaryDataAggregatorService"></property>
</bean>

这是我的 grails 服务类,我用 @Async 注释了它的方法

类 BinaryDataAggregatorService 实现 AggregateSocketData {

def rawDataService
static transactional = true

@Async
void receiveSocketData(IEvent event) {
println Thread.currentThread().name
log.debug("Going to decode netty packet in receiveSocketData");
Map decodedPacket = decodePacket((INettyPacket)event.getSource())
def rawData = saveRawData (decodedPacket);
log.debug ("Saved raw data, id: ${rawData?.id}")
rawDataService.saveHTTPData(decodedPacket);
}

}

[更新 2] 这是方法调用的堆栈跟踪。这是堆栈。 BinaryDataAggregatorService.receiveSocketData(INettyPacket) 行:20BinaryDataAggregatorService$$FastClassByCGLIB$$82489f62.invoke(int, Object, Object[]) 行:不可用
MethodProxy.invoke(Object, Object[]) 行:149
Cglib2AopProxy$CglibMethodInitation.invokeJoinpoint() 行:688
Cglib2AopProxy$CglibMethodInitation(ReflectiveMethodInitation).proceed() 行:150
TransactionInterceptor.invoke(MethodInitation) 行:110
Cglib2AopProxy$CglibMethodInitation(ReflectiveMethodInitation).proceed() 行:172
Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(对象,方法,对象[],MethodProxy)行:621BinaryDataAggregatorService$$EnhancerByCGLIB$$1c96985c.receiveSocketData(INettyPacket) 行:不可用
DefaultHandler.handlePacket(INettyPacket) 行:50

[更新3]设置任务时的 Grails 堆栈跟踪:注释驱动元素。

011-05-26 17:38:03,109 [main] ERROR context.GrailsContextLoader  - Error executing bootstraps: Error creating bean with name 'defaultECM1240Handler' defined in URL [file:./grails-app/conf/spring/resources.xml]: Cannot resolve reference to bean 'binaryDataAggregatorService' while setting bean property 'aggregateSocketData'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'binaryDataAggregatorService': Invocation of init method failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class $Proxy12]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy12
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultECM1240Handler' defined in URL [file:./grails-app/conf/spring/resources.xml]: Cannot resolve reference to bean 'binaryDataAggregatorService' while setting bean property 'aggregateSocketData'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'binaryDataAggregatorService': Invocation of init method failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class $Proxy12]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy12

谢谢,
亚伯拉罕

最佳答案

如果是 Spring Bean - 是的,可以。为此,您必须用 @Service 对其进行注释。并有一个 <context:component-scan base-package="com.foo.bar" />

但是为了更容易,您可以使用放置在grails-app/services中的groovy类- 它将自动成为一个 spring bean

为了使@Async工作,你需要<task:annotation-driven/>在 xml 配置中。

关于java - 我可以在 Grails 中使用 @Async 注释吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6120016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com