EJB无状态 - 私有成员初始化 [英] EJB stateless - Private members initialisation
问题描述
这是一个简短的例子:
@Singleton
@LocalBean
@Startup
public class Starter {
@PostActivate
private void postActivate(){
ScheduleEJB scheduleEjb = new ScheduleEJB(Hello);
}
}
豆:
@Stateless
@LocalBean
public class ScheduleEJB {
私人字符串消息;
public ScheduleEJB(){
super();
}
public ScheduleEJB(String message){
super();
this.message = message;
}
@Schedule(second =* / 3,minute =*,hour =*,dayOfMonth =*,dayOfWeek =* *,year =*)
private void printMsg(){
System.out.println(MESSAGE:+ message);
}
}
问题是我的消息变量总是在printMsg()方法中打印时为null ...有什么最好的方法来实现?
感谢您的帮助!
你在这里混合了几件事情。
-
@PostActivate 注释用于状态会话Bean(SFSB),您使用它在单身人士。我想你的意思是 @PostConstruct 方法适用于容器管理生命周期的每个bean。
-
使用您的EJB的构造函数。
ScheduleEJB scheduleEjb = new ScheduleEJB(Hello);
因为它只创建一个这个类的一个实例。它不是一个EJB - 容器没有创建它,所以这个类没有任何EJB性质。
这就是依赖注入的全部 - 你只需要定义你想要的内容,容器负责为你提供适当的资源实例。 - p> 无状态Bean(SLSB)并不意图保持状态。 SFSB是。即使您将使用一个SLSB方法(即某些ScheduleEJB#setMessage(String)方法)设置消息,而不是您需要记住 EJB的集合。您不能保证下次在ScheduleEJB上调用方法时,您将获得相同的实例。
在您的情况下,只需将@Schedule方法添加到单例类中即可。您可以在@PostConstruct方法中定义您选择的变量。您可以确保每个JVM只有一个Singleton实例,因此您的变量将在同一类的Schedule注释方法中可见。
HTH。
I'm new to EJB and I'm facing my first problem. I'm trying to use an @Schedule method contained in a Stateless EJB. I'd like this method to use a private member variable which would be set at bean creation:
Here's a short example:
@Singleton
@LocalBean
@Startup
public class Starter {
@PostActivate
private void postActivate() {
ScheduleEJB scheduleEjb = new ScheduleEJB("Hello");
}
}
And the schedule bean:
@Stateless
@LocalBean
public class ScheduleEJB {
private String message;
public ScheduleEJB() {
super();
}
public ScheduleEJB(String message) {
super();
this.message= message;
}
@Schedule(second="*/3", minute="*", hour="*", dayOfMonth="*", dayOfWeek="*", month="*", year="*")
private void printMsg() {
System.out.println("MESSAGE : " + message);
}
}
The problem is that my "message" variable is always null when printed in the printMsg() method... What's the best way to achieve this?
Thanks for your help !
You're mixing few things here.
The @PostActivate annotation is to be used on Stateful Session Beans (SFSB), and you use it on the singleton. I guess that you mean the @PostConstruct method which applies to every bean which lifecycle is managed by the container.
You're using a constructor from your EJB. You cannot do:
ScheduleEJB scheduleEjb = new ScheduleEJB("Hello");
as it creates just an instance of this class. It's not an EJB - the container didn't create it, so this class does not have any EJB nature yet. That's the whole point of dependency injection - you just define what you want and the container is responsible for providing you with an appropriate instance of the resource.
The Stateless Bean (SLSB) is not intented to hold the state. The SFSB is. Even if you would set the message in one SLSB method (i.e. in some ScheduleEJB#setMessage(String) method) than you need to remember that the EJB's are pooled. You don't have any guarantee that the next time you invoke a method on the ScheduleEJB you will get to the same instance.
In your case it would be the easies solution just to add the @Schedule method to your singleton class. Than you can define the variable of your choice in the @PostConstruct method. You can be sure that there is only one Singleton instance per JVM, so your variable will be visible in the Schedule annotated method of the same class.
HTH.
这篇关于EJB无状态 - 私有成员初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!