01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.getahead.dwrdemo.pubsub;
17:
18: import org.apache.commons.logging.Log;
19: import org.apache.commons.logging.LogFactory;
20: import org.directwebremoting.HubFactory;
21: import org.directwebremoting.event.MessageEvent;
22: import org.directwebremoting.event.MessageListener;
23: import org.directwebremoting.extend.MarshallException;
24: import org.getahead.dwrdemo.people.Person;
25:
26: /**
27: * A demo of the pub/sub side of JMS.
28: * Currently DWR does not support point-to-point JMS because there doesn't seem
29: * much point - web clients are inherently fickle, so getting the address of a
30: * browser out of JNDI seems silly
31: * @author Joe Walker [joe at getahead dot ltd dot uk]
32: */
33: public class HubTest {
34: /**
35: * Setup the hub and create a subscriber listener
36: */
37: public HubTest() {
38: HubFactory.get().subscribe(TOPIC_TEXT, new MessageListener() {
39: public void onMessage(MessageEvent message) {
40: try {
41: log.info(message.getData(String.class));
42: } catch (MarshallException ex) {
43: log.info("Failed to read data published to "
44: + TOPIC_TEXT);
45: }
46: }
47: });
48:
49: HubFactory.get().subscribe(TOPIC_PEOPLE, new MessageListener() {
50: public void onMessage(MessageEvent message) {
51: try {
52: log.info(message.getData(Person.class));
53: } catch (MarshallException ex) {
54: log.info("Failed to read data published to "
55: + TOPIC_TEXT);
56: }
57: }
58: });
59: }
60:
61: /**
62: * Exported method to publish a string to a topic
63: */
64: public void publishText(String data) {
65: String info = "JmsTest says " + data;
66: log.info("Publishing message '" + info + "' to '" + TOPIC_TEXT
67: + "'");
68: HubFactory.get().publish(TOPIC_TEXT, info);
69: }
70:
71: /**
72: * Exported method to publish a bean to a topic
73: */
74: public void publishPerson(Person person) {
75: log.info("Publishing person '" + person.getName() + "' to '"
76: + TOPIC_PEOPLE + "'");
77: HubFactory.get().publish(TOPIC_PEOPLE, person);
78: }
79:
80: /**
81: * The default implementation just hard codes a topic name
82: */
83: private static final String TOPIC_TEXT = "pubsub.topicText";
84:
85: /**
86: * The default implementation just hard codes a topic name
87: */
88: private static final String TOPIC_PEOPLE = "pubsub.topicPeople";
89:
90: /**
91: * The log stream
92: */
93: protected static final Log log = LogFactory.getLog(HubTest.class);
94: }
|