Code: Select all
package com.alsasoft.webapp.push.notify;
import javax.inject.Inject;
import org.primefaces.push.EventBus;
import org.primefaces.push.RemoteEndpoint;
import org.primefaces.push.annotation.OnClose;
import org.primefaces.push.annotation.OnMessage;
import org.primefaces.push.annotation.OnOpen;
import org.primefaces.push.annotation.PushEndpoint;
import org.primefaces.push.annotation.Singleton;
import org.primefaces.push.impl.JSONEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.alsasoft.common.jms.WMessage;
import com.alsasoft.webapp.listener.DeviceMessageListener;
@PushEndpoint("/notify")
@Singleton
public class NotifyResource {
// protected final Log log = LogFactory.getLog(getClass());
private static final Logger LOG = LoggerFactory.getLogger(NotifyResource.class);
@Inject
private DeviceMessageListener deviceMessageListener;
public void setDeviceMessageListener(DeviceMessageListener deviceMessageListener) {
this.deviceMessageListener = deviceMessageListener;
}
@OnOpen
public void onOpen(RemoteEndpoint r, EventBus e) {
LOG.debug("push onOpen");
}
@OnClose
public void onClose(RemoteEndpoint r, EventBus e) {
LOG.debug("push onClose");
deviceMessageListener.stopSubscriber(); // [b]deviceMessageListener is always NULL[/b]
}
@OnMessage(encoders = { JSONEncoder.class })
public WMessage onMessage(WMessage message) {
// log.debug(message);
LOG.debug(message.toString());
return message;
}
}
Code: Select all
package com.alsasoft.webapp.listener;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
//@ManagedBean(name="deviceMessageListener")
//@SessionScoped
//@Component
//@Scope("globalSession")
//@Qualifier("deviceMessageListener")
@Named(value="deviceMessageListener")
@ApplicationScoped
public class DeviceMessageListener implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@PostConstruct
public void startSubscriber() {
System.out.println("\n * * * DeviceMessageListener.PostConstruct\n");
}
@PreDestroy
public void stopSubscriber() {
System.out.println("\n * * * DeviceMessageListener.PreDestroy\n");
}
public void onMessage(String wmsg) {
System.out.println("\n * * * DeviceMessageListener.onMessage\n");
}
}