Push with Richfaces 4.2.1 comming from Richfaces 4.0.0-Final
haukegulich May 3, 2012 3:14 PMHello everyone,
I was so glad finally to get the push function to work with richfaces 4.0.0-Final (thanks to this board).
Last week I tried to add a context-menu to a graphic and I noticed that within 4.0.0-Final no <rich:contextMenu> was shipped.
So I upgraded to 4.2.1-Final (with maven) and the context-menu is working.
But the push function isn't working anymore.
What I did with the 'old' release was the following:
1) added this to faces-config.xml
<application> | |||
<system-event-listener> | |||
<system-event-listener-class>de.hauke.client.system.TopicsInitializer</system-event-listener-class> | |||
<system-event-class>javax.faces.event.PostConstructApplicationEvent</system-event-class> | |||
</system-event-listener> | |||
</application> |
2) added the the mentioned class above (TopicsInitializer.java)
{code}
public class TopicsInitializer extends SessionBeanBase implements SystemEventListener, MessageListener {
private static final Logger LOG = Logger.getLogger(TopicsInitializer.class);
private transient TopicsContext topicsContext;
private transient Context ctx;
public static enum SubTopic {
LIGHT,
TEMPERATUR,
}
private static final Map<SubTopic, String> subTopicMapping = new HashMap<SubTopic, String>() {{
put(SubTopic.LIGHT, "light");
put(SubTopic.TEMPERATUR, "temperature");
}};
public TopicsInitializer() {
init();
}
private void init() {
LOG.info("Der Client hoert auf Die Queue toClient : " + ConfigurationHelper.CONFIGKEY_JMS_QUEUE_TOCLIENT);
try {
// Queuenamen aus der Datenbank auslesen
String queueName = getInterfaceHelper().getConfigurationHelper().getValue(ConfigurationHelper.CONFIGKEY_JMS_QUEUE_TOCLIENT);
QueueConnectionFactory connectionFactory = (QueueConnectionFactory) getInitialContext().lookup("java:/ConnectionFactory");
QueueConnection connection = connectionFactory.createQueueConnection();
Queue queue = (Queue) getInitialContext().lookup(queueName);
QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
connection.start();
// Diese Klasse als Listener registrieren
session.createReceiver(queue).setMessageListener(this);
} catch (Exception e) {
LOG.error(ExceptionUtils.getFullStackTrace(e));
}
}
public void processEvent(SystemEvent event) throws AbortProcessingException {
LOG.info("TopicsInitializer - ProcessEvent wurde aufgerufen - SubTopics werden konfiguriert");
topicsContext = TopicsContext.lookup();
// Jedes SubTopic erzeugen aus der Enumeration
for (SubTopic subTopic : SubTopic.values()) {
Topic topic = topicsContext.getOrCreateTopic(new TopicKey("notify", subTopicMapping.get(subTopic)));
topic.setMessageDataSerializer(DefaultMessageDataSerializer.instance());
topic.addTopicListener(new SessionTopicListener() {
@Override
public void processPreSubscriptionEvent(SessionPreSubscriptionEvent event) throws EventAbortedException {
}
@Override
public void processSubscriptionEvent(SessionSubscriptionEvent event) throws EventAbortedException {
}
@Override
public void processUnsubscriptionEvent(SessionUnsubscriptionEvent event) throws EventAbortedException {
}
});
}
}
public boolean isListenerForSource(Object source) {
return true;
}
private Context getInitialContext() {
try {
if (ctx == null) {
ctx = new InitialContext();
}
} catch (NamingException e) {
}
return ctx;
}
@Override
public void onMessage(Message message) {
TextMessage tm = (TextMessage) message;
try {
LOG.info("Client hat eine Nachricht empfangen : " + tm.getText());
HomematicDeviceName device = HomematicDeviceName.valueOf(tm.getText());
if (device == null) {
LOG.error("Unbekannte Nachricht!!!");
return;
}
if (device.name().startsWith("LICHT")) {
notifyClient(SubTopic.LIGHT);
} else if (device.name().startsWith("TEMP")) {
notifyClient(SubTopic.TEMPERATUR);
}
} catch (Exception e) {
LOG.error(ExceptionUtils.getFullStackTrace(e));
}
}
private void notifyClient(SubTopic subTopic) throws MessageException {
LOG.info("Sende Nachricht an das SubTopic : " + subTopicMapping.get(subTopic));
org.richfaces.application.push.TopicKey topicKey = new org.richfaces.application.push.TopicKey("notify", subTopicMapping.get(subTopic));
topicsContext.publish(topicKey, " ");
}
{code}
I removed some comments and some other not useful stuff here.
Since 4.2.1 some classes are deprecated like SessionTopicListener, EventAbortedException, EventAbortedException and EventAbortedException
Like I said, it was kind of hard to get this working so it might be a little bit confusing my code but unfortunately the examples of push aren't very easy to understand inside the download of richfaces. There are > 10 classes and interfaces.
Hopefully someday there is something much more easier example :-)
Thanks,
Hauke
PS.: I don't get any exception. It's only not working. No refreshes comming up.