作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在工作 CDI Pushevent。我计划通过后端Java代码而不是前端jsf通过commandButton触发pushevent。这是PushCdiBean.java的代码
import java.io.Serializable;
import java.util.UUID;
import javax.annotation.PostConstruct;
import javax.enterprise.event.Event;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import org.richfaces.application.push.MessageException;
import org.richfaces.application.push.TopicKey;
import org.richfaces.application.push.TopicsContext;
import org.richfaces.cdi.push.Push;
/**
* @author <a href="http://community.jboss.org/people/lfryc">Lukas Fryc</a>
*/
@javax.inject.Named("pushCdiBean")
@javax.enterprise.context.RequestScoped
//@ManagedBean(name="pushCdiBean")
//@ViewScoped
public class PushCdiBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5241937306040858158L;
private static final String CDI_PUSH_TOPIC = "pushCdi";
private String userIdentifier;
private String message;
@Inject
@Push(topic=CDI_PUSH_TOPIC)//i thought that the topic is initialized with this ?!
private Event<String> pushEvent;
@PostConstruct
public void initialize() {
if(userIdentifier == null) {
userIdentifier = UUID.randomUUID().toString().replace("-", "");
}
TopicsContext topicsContext = TopicsContext.lookup();
topicsContext.getOrCreateTopic(new TopicKey(CDI_PUSH_TOPIC, userIdentifier));//initialize the topic and make the troublesome message disappears
}
public synchronized void sendMessage() throws MessageException {
pushEvent.fire(message);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Event<String> getPushEvent() {
return pushEvent;
}
public void setPushEvent(Event<String> pushEvent) {
this.pushEvent = pushEvent;
}
public String getUserIdentifier() {
return userIdentifier;
}
public void setUserIdentifier(String userIdentifier) {
this.userIdentifier = userIdentifier;
}
但是,当我尝试创建 PushCdiBean 类并调用 sendMessage() 函数时,pushEvent.fire 失败。代码在这里。
/**
*
*/
public void run() {
while (running) {
try {
PushCdiBean pushTest = new PushCdiBean();
pushTest.setMessage("This is CDI push Test");
pushTest.sendMessage();
}
catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
sleep(4000);
}
}
您能给我一些建议吗?非常感谢。
最佳答案
您必须@Inject
CDI bean,而不是通过 new 自行实例化它。
关于javax.enterprise.event.Event : How to initialize the pushEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13936582/
我是一名优秀的程序员,十分优秀!