• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

moto web application


Commit MetaInfo

修訂00db3ab17a4ba890862b9a70bacb87a5bf07d528 (tree)
時間2014-01-22 19:17:38
作者astoria-d <astoria-d@mail...>
Commiterastoria-d

Log Message

jms push integrated. still need to work due to the invalid instance scope

Change Summary

差異

--- a/WEB-INF/classes/motoSample/ChatBean.java
+++ b/WEB-INF/classes/motoSample/ChatBean.java
@@ -179,7 +179,7 @@ public class ChatBean implements Serializable {
179179 catch (SQLException se) {
180180 log.severe("sql err!!!");
181181 }
182-
182+
183183 }
184184
185185 public void loadOldMsg() {
@@ -235,6 +235,7 @@ public class ChatBean implements Serializable {
235235
236236 @PostConstruct
237237 public void postInit() {
238+ log.info("postInit");
238239 //log.info("PostConstruct userBean: " + userBean);
239240 //log.info("PostConstruct getFlights: " + userBean.getFlights());
240241 ArrayList<SelectItem> flights = userBean.getFlights();
@@ -252,6 +253,6 @@ public class ChatBean implements Serializable {
252253 }
253254 }
254255
255- //log.info("PostConstruct: " + chatRoom);
256256 }
257+
257258 }
--- a/WEB-INF/classes/motoSample/PushBean.java
+++ b/WEB-INF/classes/motoSample/PushBean.java
@@ -1,46 +1,162 @@
11 package motoSample;
22
33 import java.io.Serializable;
4-import java.util.logging.Logger;
5-import javax.inject.Inject;
6-
74 import javax.faces.bean.ManagedBean;
85 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;
914
1015 import org.richfaces.application.push.TopicKey;
1116 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;
1237
1338
1439 @ManagedBean
1540 @RequestScoped
16-public class PushBean implements Serializable {
41+public class PushBean implements MessageListener, Serializable {
1742
1843 @Inject
1944 private Logger log;
2045
21- private String msg;
46+ @Inject
47+ private FacesContext context;
48+
49+ private static final String PUSH_JMS_TOPIC = "motoTest";
2250
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+
27104 }
28105
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: ");
41108 /*
109+ TopicKey topicKey = new TopicKey(chatRoom);
110+ TopicsContext topicsContext = TopicsContext.lookup();
111+
112+ topicsContext.publish(topicKey, "empty message");
42113 */
43114
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());
44122 }
45123
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+}
--- a/WEB-INF/web.xml
+++ b/WEB-INF/web.xml
@@ -50,7 +50,7 @@
5050 <param-value>true</param-value>
5151 </context-param>
5252 <context-param>
53- <param-name>org.richfaces.push.jms.disabled</param-name>
53+ <param-name>org.richfaces.push.jms.enabled</param-name>
5454 <param-value>true</param-value>
5555 </context-param>
5656 <context-param>
--- a/build.xml
+++ b/build.xml
@@ -26,6 +26,7 @@
2626 <property name="servlet" value="${env.JBOSS_HOME}\modules\javax\servlet\api\main\jboss-servlet-api_3.0_spec-1.0.0.Final.jar"/>
2727 <property name="cdi" value="${env.JBOSS_HOME}\modules\javax\enterprise\api\main\cdi-api-1.0-SP4.jar"/>
2828 <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"/>
2930
3031 <target name="clean">
3132 <!-- Delete our the ${build} and ${dist} directory trees -->
@@ -47,7 +48,7 @@
4748 srcdir="${javasrc}"
4849 destdir="${build}"
4950 debug="true"
50- classpath="${jsf}:${inject}:${servlet}:${cdi}:${richfaces}"/>
51+ classpath="${jsf}:${inject}:${servlet}:${cdi}:${richfaces}:${jms}"/>
5152
5253 </target>
5354
--- a/secured/chat.xhtml
+++ b/secured/chat.xhtml
@@ -14,16 +14,9 @@
1414 <h:form xmlns:a4j="http://richfaces.org/a4j"
1515 xmlns:rich="http://richfaces.org/rich">
1616
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 -->
2720
2821 chat room :
2922 <h:selectOneMenu id="charRoom" value="#{chatBean.chatRoom}" onchange="submit()"
@@ -64,7 +57,7 @@
6457 input text: <h:inputText id="msg" value="#{chatBean.msg}"/>
6558 </h:panelGroup>
6659
67- <h:commandButton value="post" action="#{chatBean.doPost}" >
60+ <h:commandButton value="post" action="#{chatBean.doPost}" actionListener="#{pushBean.doPush}" >
6861 <f:ajax execute="charRoom msg" render="chatTable" />
6962 </h:commandButton>
7063 </h:form>