fix bean creation cycle problem

This commit is contained in:
Mengyi Zhou 2015-07-10 15:08:51 +08:00
parent e1d369c744
commit 92269b2cfa
7 changed files with 49 additions and 20 deletions

View file

@ -5,6 +5,8 @@ import com.ctrip.zeus.service.report.ReportService;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -17,21 +19,31 @@ import javax.annotation.Resource;
@Component @Component
public class ReportAspect implements Ordered { public class ReportAspect implements Ordered {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource @Resource
private ReportService reportService; private ReportService reportService;
@Around("execution(* com.ctrip.zeus.service.model.GroupRepository.*(..))") @Around("execution(* com.ctrip.zeus.service.model.GroupRepository.*(..))")
public Object injectReportAction(ProceedingJoinPoint point) throws Throwable { public Object injectReportAction(ProceedingJoinPoint point) throws Throwable {
String methodName = point.getSignature().getName(); String methodName = point.getSignature().getName();
boolean isNew;
switch (methodName) { switch (methodName) {
case "add": case "add":
isNew = true;
break;
case "update": case "update":
isNew = false;
break; break;
default: default:
return point.proceed(); return point.proceed();
} }
Object obj = point.proceed(); Object obj = point.proceed();
reportService.reportGroup((Group)obj); try {
reportService.reportGroup((Group) obj, isNew);
} catch (Exception ex) {
logger.error("Fail to report group to queue.", ex);
}
return obj; return obj;
} }

View file

@ -7,9 +7,5 @@ import com.ctrip.zeus.model.entity.Group;
*/ */
public interface ReportService { public interface ReportService {
void reportGroup(Group group) throws Exception; void reportGroup(Group group, boolean isNew) throws Exception;
void sync() throws Exception;
boolean needSync() throws Exception;
} }

View file

@ -0,0 +1,11 @@
package com.ctrip.zeus.service.report;
/**
* Created by zhoumy on 2015/7/10.
*/
public interface ReportSyncService {
void sync() throws Exception;
boolean needSync() throws Exception;
}

View file

@ -10,17 +10,7 @@ import org.springframework.stereotype.Component;
@Component("mockReportService") @Component("mockReportService")
public class MockReportService implements ReportService { public class MockReportService implements ReportService {
@Override @Override
public void reportGroup(Group group) throws Exception { public void reportGroup(Group group, boolean isNew) throws Exception {
} }
@Override
public void sync() throws Exception {
}
@Override
public boolean needSync() throws Exception {
return false;
}
} }

View file

@ -0,0 +1,18 @@
package com.ctrip.zeus.service.report.impl;
import com.ctrip.zeus.service.report.ReportSyncService;
/**
* Created by zhoumy on 2015/7/10.
*/
public class MockReportSyncService implements ReportSyncService {
@Override
public void sync() throws Exception {
}
@Override
public boolean needSync() throws Exception {
return false;
}
}

View file

@ -3,11 +3,12 @@
<entity name="report" table="report" alias="l" do-class="ReportDo"> <entity name="report" table="report" alias="l" do-class="ReportDo">
<query-defs> <query-defs>
<query name="find-by-group" type="SELECT"> <query name="find-by-group" type="SELECT">
<param name="group-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='group-id'/> = <FIELD name='group-id'/> WHERE <FIELD name='group-id'/> = ${group-id}
]]> ]]>
</statement> </statement>
</query> </query>
@ -16,7 +17,7 @@
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='current-version'/> > <FIELD name='reported-version'/> WHERE current_version > reported_version
]]> ]]>
</statement> </statement>
</query> </query>

View file

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<model model-package="com.ctrip.zeus.report" /> <model model-package="com.ctrip.zeus.report" enable-sax-parser="true" enable-json-parser="true"
enable-json-builder="true"/>