[英]JPA.withTransaction executing other controllers method error: Global.java:39: error: 'void' type not allowed here
I am trying to execute some db insert/update queries in some time intervals. 我试图在某些时间间隔执行一些数据库插入/更新查询。
To achive this I'decided to use Playframework built in Akka Actor system. 为此,我决定使用Akka Actor系统中内置的Playframework。
I have my class with method: 我上课有方法:
public class Global extends GlobalSettings {
@Override
public void onStart(Application application) {
Akka.system().scheduler().schedule(
Duration.create(1, TimeUnit.SECONDS), // start task delay
Duration.create(24, TimeUnit.HOURS), // between task instance delay
//Duration.create(24, TimeUnit.HOURS), // between task instance delay
new Runnable() {
@Override
public void run() {
JPA.withTransaction(
ImportCrmData.start()
);
}
},
Akka.system().dispatcher()
);
And the method that is called by the actor system: 以及actor系统调用的方法:
public class ImportCrmData extends Controller {
@Transactional
public static void start() {
List<Customer> customersToUpdate = CustomerCRM.importCrmData();
for(Customer c: customersToUpdate) {
c.toDataBase();
}
}
}
I am getting an error on compile: 我在编译时遇到错误:
[error] app/modules/common/app/Global.java:39: error: 'void' type not allowed here ImportCrmData.start()
I understand that the problem occurs cause JPA.withTransaction() demand me to return from ImportCrmData.start()
return Callback0
or Function0<>
, but I dont know how to do it. 我知道发生此问题是因为JPA.withTransaction()要求我从
ImportCrmData.start()
返回Callback0
或Function0<>
,但我不知道该怎么做。
My method is just doing this.persist
. 我的方法就是这样做
this.persist
。 Why should I even return something from that? 我为什么还要从中退回一些东西?
ImportCrmData
is a controller thus it must return a valid http response (a result). ImportCrmData
是一个控制器,因此它必须返回有效的http响应(结果)。 A typical use case: 典型的用例:
public class CustomerController extends Controller {
public static Result getCustomers() {
List<Customer> customers = CustomerService.getCustomers();
return ok(Json.toJson(customers));
}
}
Above example consists of controller which is an entry point to your application and reacts with client requests. 上面的示例由控制器组成,该控制器是您应用程序的入口点,并响应客户端请求。
CustomerService
encapsulates logic related to getting customers. CustomerService
封装了与获取客户有关的逻辑。 ok(...)
returns an implementation of Result
- a valid http response with code 200 and in above scenario, json body. ok(...)
返回Result
的实现-一个有效的http响应,代码为200,在上述情况下为json主体。 It is implemented in Controller
base class. 它在
Controller
基类中实现。 Next your controller can be mapped in routes
file to a url like so: 接下来,您的控制器可以在
routes
文件中映射到如下网址:
GET /customers controller.CustomerController.getCustomers
Applying above pattern
you should have: 应用以上
pattern
您应该具有:
This separation allows using your CrmService
in Global
class, as well as in Controller
layer without duplicating logic. 这种分离允许在
Global
类以及Controller
层中使用CrmService
,而无需重复逻辑。 Mind this is just a suggestion. 注意,这只是一个建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.