根據以下彈簧文檔link我可以使用@Async註釋使方法調用異步。我可以從我擁有的java src文件在Grails中使用此工具嗎?我可以在Grails中使用@Async註釋
[更新] 這是我的java(netty)套接字處理程序類,它接收套接字數據包。
公共類的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 id="defaultECM1240Handler" class="com.appcapture.buildingmgr.netty.DefaultHandler"
scope="prototype">
<property name="aggregateSocketData" ref="binaryDataAggregatorService"></property>
</bean>
,這是我的Grails服務類,其方法我用@Async註解
class BinaryDataAggregatorService implements 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)線:20 BinaryDataAggregatorService $$ FastClassByCGLIB $$ 82489f62.invoke(INT,對象,對象[])行:不可用
MethodProxy.invoke(對象,對象[])線:149
Cglib2AopProxy $ CglibMethodInvocation.invokeJoinpoint()線:688
Cglib2AopProxy $ CglibMethodInvocation(ReflectiveMethodInvocation).proceed()線:150
TransactionInterceptor.invoke(的MethodInvocation)線:110
Cglib2AopProxy $ CglibMethodInvocation(ReflectiveMethodInvocation).proceed()線:172
Cglib2AopProxy $ DynamicAdvisedInterceptor.intercept(Object,Method,Object [],MethodProx y)的行:621 BinaryDataAggregatorService $$ 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
謝謝
亞伯拉罕
我添加了註釋到服務類,但在調試我無法看到另一個線程已經建立。我需要提供其他一些配置嗎? – Abe 2011-05-25 08:11:19
不是我所知道的。嘗試打印當前線程ID以查看它們是否相同。而且 - 你如何獲得java類的實例? – Bozho 2011-05-25 08:37:57
我沒有將註釋添加到Java類,而是將其添加到grails服務方法。既然它是默認的spring bean,我認爲它會起作用。當我印刷它時,我得到了相同的線名。 – Abe 2011-05-25 09:56:11