a4j push - bind to jms topic
mglowacki Mar 15, 2010 5:57 AMhi,
I have read that it is possible to connect a4j push component to jms. I try to make a messaging system for customers - when one producer publishes the message, all logged in customers should get this message throught a4j:push component.
Assuming my MDB works fine, here's the code snippet for my class used by a4j:push:
@Name("alertsPush")
public class AlertsPush implements javax.jms.MessageListener, Serializable {
/**
*
*/
private static final long serialVersionUID = -7549165554107841679L;
@Logger
private Log log;
PushEventListener listener;
private long lastAlertTimestamp;
private ArrayList<AlertModel> alertsList;
public AlertsPush() throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.SECURITY_AUTHENTICATION, "none");
Context context = new InitialContext(env);
TopicConnectionFactory factory = (TopicConnectionFactory)
context.lookup("ConnectionFactory");
Topic topic = (Topic)context.lookup("topic/Alerts");
TopicConnection connect = factory.createTopicConnection();
TopicSession session =
connect.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createSubscriber(topic);
subscriber.setMessageListener(this);
connect.start();
}
public void addListener(EventListener listener) {
synchronized (listener) {
if (this.listener != listener) {
this.listener = (PushEventListener) listener;
}
}
}
public void onMessage(Message arg0) {
log.error("ALERT RECEIVED");
try {
updateAlertsList();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listener.onEvent(new EventObject(this));
}
private void updateAlertsList() throws Exception {
AlertsCache cache = AlertsCache.getInstance();
this.alertsList = cache.getAlerts();
}
public void setAlertsList(ArrayList<AlertModel> alertsList) {
this.alertsList = alertsList;
}
public ArrayList<AlertModel> getAlertsList() {
return alertsList;
}
}
And XHMTL code for this component is as follows:
<a4j:push interval="5000" eventProducer="#{alertsPush.addListener}" reRender="alertsTable" enabled="true" id="alertsPush"/>
unfortunaly the line:
listener.onEvent(new EventObject(this));
throws RuntimeException (NullPointerException) when I publish an alert. I don't know how to register listener when I want to use Message Driven Bean (topic). Anyone could give me a hint?