我找到了解决问题的方法。我在这里留下了我找到的解决方案,第一个: 1.为了测试解决方案,我使用了Glassfish 3.1.2.2而不是Tomcat 7,Netbeans 7.3和PrimeFaces 3.5 2.我根据这个tutorial做了一个示例应用程序。 3.我实现了PrimeFaces演示页面的PimePush Counter示例。 4.我做了一个Swing应用程序,用一个按钮来模拟串行端口捕捉的动作,这个应用程序使用ws来推送Web应用程序中的内容。 5. GF支持启用彗星和Web套接字。
但是,有一个问题,每次我重新启动Glassfish时,都需要取消部署应用程序,并且他们需要重新部署才能使Web服务可用。为什么这个?,我错过了一些事件?
下面的代码:
的WS类:
@WebService(serviceName = "BotonService")
@Stateless
public class BotonService {
private boolean push = false;
@EJB
private ServiceShare servicio;
/**
* This is a sample web service operation
*/
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
/**
* Web service operation
*/
@WebMethod(operationName = "setPush")
public void setPush(@WebParam(name = "push") boolean push) {
this.push = push;
servicio.setPush(push);
servicio.firePush();
}
/**
* Web service operation
*/
@WebMethod(operationName = "getPush")
public Boolean getPush() {
return this.push;
}
}
这里ManagedBean类:
@Stateless
@ManagedBean(name = "globalCounter")
@ApplicationScoped
public class GlobalCounterBean implements Serializable {
private int count;
private String CHANNEL = "/counter";
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public synchronized void increment() {
count++;
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
pushContext.push(CHANNEL, String.valueOf(count));
}
}
而且这里的EJB使用与Managedbean沟通WS:
@Stateless
public class ServiceShare {
@EJB
private GlobalCounterBean counter;
private boolean push = false;
public boolean getPush() {
return push;
}
public void setPush(boolean push){
this.push = push;
}
public void firePush(){
if(this.push){
counter.increment();
}
}
}
JSF页面与PrimeFaces演示中的页面完全相同。
每当我按下我制作的摇摆应用程序中的按钮,计数器就会在每台连接的机器上更新,这就是最终应用程序的想法。但是,为什么我必须重新部署Web应用程序,以便swing应用程序中的ws-client可以找到它?有没有我在GF服务器中缺少的配置来避免这种行为?
看起来合理 - Primefaces拥有[comet support](http://www.primefaces.org/showcase/ui/home.jsf),我猜这将是至关重要的,因为服务器需要通知clinets关于异步事件。 – dratewka 2013-05-10 14:04:59
好的,但我该如何将异步事件发送到Web应用程序? – 2013-05-10 14:13:38
那么这取决于这些事件的数量。一种可能性是创建托管在您的服务器上的Web服务,该应用程序可以调用该服务来通知事件。另一种可能性是在Web服务器的单独线程上运行端口监视逻辑。 – dratewka 2013-05-10 20:04:13