moto web application
修訂 | 00db3ab17a4ba890862b9a70bacb87a5bf07d528 (tree) |
---|---|
時間 | 2014-01-22 19:17:38 |
作者 | astoria-d <astoria-d@mail...> |
Commiter | astoria-d |
jms push integrated. still need to work due to the invalid instance scope
@@ -179,7 +179,7 @@ public class ChatBean implements Serializable { | ||
179 | 179 | catch (SQLException se) { |
180 | 180 | log.severe("sql err!!!"); |
181 | 181 | } |
182 | - | |
182 | + | |
183 | 183 | } |
184 | 184 | |
185 | 185 | public void loadOldMsg() { |
@@ -235,6 +235,7 @@ public class ChatBean implements Serializable { | ||
235 | 235 | |
236 | 236 | @PostConstruct |
237 | 237 | public void postInit() { |
238 | + log.info("postInit"); | |
238 | 239 | //log.info("PostConstruct userBean: " + userBean); |
239 | 240 | //log.info("PostConstruct getFlights: " + userBean.getFlights()); |
240 | 241 | ArrayList<SelectItem> flights = userBean.getFlights(); |
@@ -252,6 +253,6 @@ public class ChatBean implements Serializable { | ||
252 | 253 | } |
253 | 254 | } |
254 | 255 | |
255 | - //log.info("PostConstruct: " + chatRoom); | |
256 | 256 | } |
257 | + | |
257 | 258 | } |
@@ -1,46 +1,162 @@ | ||
1 | 1 | package motoSample; |
2 | 2 | |
3 | 3 | import java.io.Serializable; |
4 | -import java.util.logging.Logger; | |
5 | -import javax.inject.Inject; | |
6 | - | |
7 | 4 | import javax.faces.bean.ManagedBean; |
8 | 5 | import javax.faces.bean.RequestScoped; |
6 | +import javax.faces.bean.ManagedProperty; | |
7 | +import javax.faces.context.FacesContext; | |
8 | +import javax.faces.event.ActionEvent; | |
9 | + | |
10 | +import java.util.logging.Logger; | |
11 | +import javax.inject.Inject; | |
12 | +import javax.annotation.PostConstruct; | |
13 | +import javax.annotation.PreDestroy; | |
9 | 14 | |
10 | 15 | import org.richfaces.application.push.TopicKey; |
11 | 16 | import org.richfaces.application.push.TopicsContext; |
17 | +import org.richfaces.application.push.MessageException; | |
18 | + | |
19 | +import javax.jms.MessageListener; | |
20 | +import javax.jms.Message; | |
21 | +import javax.jms.TextMessage; | |
22 | + | |
23 | +import javax.naming.Context; | |
24 | +import javax.naming.InitialContext; | |
25 | +import javax.naming.NamingException; | |
26 | + | |
27 | +import java.util.Properties; | |
28 | + | |
29 | +import javax.jms.TopicConnectionFactory; | |
30 | +import javax.jms.TopicConnection; | |
31 | +import javax.jms.Topic; | |
32 | +import javax.jms.Session; | |
33 | +import javax.jms.TopicSession; | |
34 | +import javax.jms.TopicSubscriber; | |
35 | +import javax.jms.TopicPublisher; | |
36 | +import javax.jms.JMSException; | |
12 | 37 | |
13 | 38 | |
14 | 39 | @ManagedBean |
15 | 40 | @RequestScoped |
16 | -public class PushBean implements Serializable { | |
41 | +public class PushBean implements MessageListener, Serializable { | |
17 | 42 | |
18 | 43 | @Inject |
19 | 44 | private Logger log; |
20 | 45 | |
21 | - private String msg; | |
46 | + @Inject | |
47 | + private FacesContext context; | |
48 | + | |
49 | + private static final String PUSH_JMS_TOPIC = "motoTest"; | |
22 | 50 | |
23 | - static int get_cnt = 0; | |
24 | - public String getMsg() { | |
25 | - msg = "msg: " + get_cnt++; | |
26 | - return msg; | |
51 | + private TopicConnectionFactory getTopicConnectionFactory() { | |
52 | + try { | |
53 | + return (TopicConnectionFactory) InitialContext.doLookup("java:/ConnectionFactory"); | |
54 | + } catch (NamingException e) { | |
55 | + try { | |
56 | + return (TopicConnectionFactory) InitialContext.doLookup("ConnectionFactory"); | |
57 | + } catch (NamingException e2) { | |
58 | + throw new IllegalStateException("Can't find registered ConnectionFactory"); | |
59 | + } | |
60 | + } | |
61 | + } | |
62 | + | |
63 | + | |
64 | + private TopicConnection connection = null; | |
65 | + private TopicSession session = null; | |
66 | + private TopicPublisher publisher = null; | |
67 | + private TopicSubscriber subscriber = null; | |
68 | + | |
69 | + @PostConstruct | |
70 | + public void initJms() throws JMSException, NamingException { | |
71 | + | |
72 | + log.info("initJms"); | |
73 | + | |
74 | + if (connection == null) { | |
75 | + TopicConnectionFactory tcf = getTopicConnectionFactory(); | |
76 | + connection = tcf.createTopicConnection(); | |
77 | + log.info("connection ok."); | |
78 | + } | |
79 | + if (session == null) { | |
80 | + session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
81 | + log.info("session ok."); | |
82 | + } | |
83 | + | |
84 | + Topic topic = InitialContext.doLookup("topic/" + PUSH_JMS_TOPIC); | |
85 | + log.info("topic ok."); | |
86 | + | |
87 | + if (publisher == null) { | |
88 | + publisher = session.createPublisher(topic); | |
89 | + log.info("publisher ok."); | |
90 | + } | |
91 | + if (subscriber== null) { | |
92 | + subscriber = session.createSubscriber(topic); | |
93 | + log.info("subscriber ok."); | |
94 | + | |
95 | + // Set a JMS message listener | |
96 | + subscriber.setMessageListener(this); | |
97 | + log.info("lisntener bind ok."); | |
98 | + } | |
99 | + | |
100 | + // Start the JMS connection; allows messages to be delivered | |
101 | + connection.start( ); | |
102 | + log.info("connection start ok."); | |
103 | + | |
27 | 104 | } |
28 | 105 | |
29 | - public void doPush() throws Exception { | |
30 | - log.info("push..."); | |
31 | - try { | |
32 | - TopicKey topicKey = new TopicKey("motoTestTopic"); | |
33 | - TopicsContext topicsContext = TopicsContext.lookup(); | |
34 | - | |
35 | - topicsContext.publish(topicKey, "empty message"); | |
36 | - } | |
37 | - catch (Exception e) { | |
38 | - System.out.println(e.getMessage()); | |
39 | - throw e; | |
40 | - } | |
106 | + public void doPush(ActionEvent event) throws MessageException, JMSException { | |
107 | + log.info("push: "); | |
41 | 108 | /* |
109 | + TopicKey topicKey = new TopicKey(chatRoom); | |
110 | + TopicsContext topicsContext = TopicsContext.lookup(); | |
111 | + | |
112 | + topicsContext.publish(topicKey, "empty message"); | |
42 | 113 | */ |
43 | 114 | |
115 | + TextMessage message = session.createTextMessage(); | |
116 | + message.setText("msgggggg"); | |
117 | + publisher.publish(message); | |
118 | + | |
119 | + //String uid = userBean.getUid(); | |
120 | + //log.info("message "); | |
121 | + log.info("message published by " + this.hashCode()); | |
44 | 122 | } |
45 | 123 | |
46 | -} | |
\ No newline at end of file | ||
124 | + public void onMessage(Message message) { | |
125 | + //String uid = userBean.getUid(); | |
126 | + log.info("message received by " + this.hashCode()); | |
127 | + | |
128 | + } | |
129 | + | |
130 | + @PreDestroy | |
131 | + public void preDestroy() { | |
132 | + log.info("preDestroy"); | |
133 | + if (publisher != null) { | |
134 | + try { | |
135 | + publisher.close(); | |
136 | + } catch (JMSException e) { | |
137 | + log.severe("unable to close publisher"); | |
138 | + } | |
139 | + } | |
140 | + if (subscriber != null) { | |
141 | + try { | |
142 | + subscriber.close(); | |
143 | + } catch (JMSException e) { | |
144 | + log.severe("unable to close subscriber"); | |
145 | + } | |
146 | + } | |
147 | + if (session != null) { | |
148 | + try { | |
149 | + session.close(); | |
150 | + } catch (JMSException e) { | |
151 | + log.severe("unable to close session"); | |
152 | + } | |
153 | + } | |
154 | + if (connection != null) { | |
155 | + try { | |
156 | + connection.close(); | |
157 | + } catch (JMSException e) { | |
158 | + log.severe("unable to close connection"); | |
159 | + } | |
160 | + } | |
161 | + } | |
162 | +} |
@@ -50,7 +50,7 @@ | ||
50 | 50 | <param-value>true</param-value> |
51 | 51 | </context-param> |
52 | 52 | <context-param> |
53 | - <param-name>org.richfaces.push.jms.disabled</param-name> | |
53 | + <param-name>org.richfaces.push.jms.enabled</param-name> | |
54 | 54 | <param-value>true</param-value> |
55 | 55 | </context-param> |
56 | 56 | <context-param> |
@@ -26,6 +26,7 @@ | ||
26 | 26 | <property name="servlet" value="${env.JBOSS_HOME}\modules\javax\servlet\api\main\jboss-servlet-api_3.0_spec-1.0.0.Final.jar"/> |
27 | 27 | <property name="cdi" value="${env.JBOSS_HOME}\modules\javax\enterprise\api\main\cdi-api-1.0-SP4.jar"/> |
28 | 28 | <property name="richfaces" value="${lib}\richfaces-components-api-4.3.4.Final.jar:${lib}\richfaces-components-ui-4.3.4.Final.jar:${lib}\richfaces-core-api-4.3.4.Final.jar:${lib}\richfaces-core-impl-4.3.4.Final.jar"/> |
29 | + <property name="jms" value="${env.JBOSS_HOME}\modules\javax\jms\api\main\jboss-jms-api_1.1_spec-1.0.0.Final.jar"/> | |
29 | 30 | |
30 | 31 | <target name="clean"> |
31 | 32 | <!-- Delete our the ${build} and ${dist} directory trees --> |
@@ -47,7 +48,7 @@ | ||
47 | 48 | srcdir="${javasrc}" |
48 | 49 | destdir="${build}" |
49 | 50 | debug="true" |
50 | - classpath="${jsf}:${inject}:${servlet}:${cdi}:${richfaces}"/> | |
51 | + classpath="${jsf}:${inject}:${servlet}:${cdi}:${richfaces}:${jms}"/> | |
51 | 52 | |
52 | 53 | </target> |
53 | 54 |
@@ -14,16 +14,9 @@ | ||
14 | 14 | <h:form xmlns:a4j="http://richfaces.org/a4j" |
15 | 15 | xmlns:rich="http://richfaces.org/rich"> |
16 | 16 | |
17 | - <h:commandButton value="post" action="#{pushBean.doPush}" > | |
18 | - <f:ajax /> | |
19 | - </h:commandButton> | |
20 | - | |
21 | - <a4j:push id="pushJms" address="motoTestTopic"> | |
22 | - <a4j:ajax event="dataavailable" render="pushRes" /> | |
23 | - </a4j:push> | |
24 | - | |
25 | - res: <h:outputText id="pushRes" value="#{pushBean.msg}"/> | |
26 | - <br /> | |
17 | + <!-- a4j:push address="#{chatBean.chatRoom}"> | |
18 | + <a4j:ajax event="dataavailable" render="msgList" /> | |
19 | + </a4j:push --> | |
27 | 20 | |
28 | 21 | chat room : |
29 | 22 | <h:selectOneMenu id="charRoom" value="#{chatBean.chatRoom}" onchange="submit()" |
@@ -64,7 +57,7 @@ | ||
64 | 57 | input text: <h:inputText id="msg" value="#{chatBean.msg}"/> |
65 | 58 | </h:panelGroup> |
66 | 59 | |
67 | - <h:commandButton value="post" action="#{chatBean.doPost}" > | |
60 | + <h:commandButton value="post" action="#{chatBean.doPost}" actionListener="#{pushBean.doPush}" > | |
68 | 61 | <f:ajax execute="charRoom msg" render="chatTable" /> |
69 | 62 | </h:commandButton> |
70 | 63 | </h:form> |