• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

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

moto web application


Commit MetaInfo

修订版9cff5fc9af03e5a87470e20aa16a25a46fb0ddd8 (tree)
时间2014-01-24 16:45:10
作者astoria-d <astoria-d@mail...>
Commiterastoria-d

Log Message

inter browsers ajax push notification updated.

更改概述

差异

--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ deleted/*
44 this-proj-is-jsf2.0-named-annotation.txt
55 data-source-memo.txt
66 aaa.txt
7+jboss.cmd-shortcut.lnk
--- a/WEB-INF/classes/motoSample/ChatBean.java
+++ b/WEB-INF/classes/motoSample/ChatBean.java
@@ -6,6 +6,7 @@ import javax.faces.bean.ViewScoped;
66 import javax.faces.bean.ManagedProperty;
77 import javax.faces.context.FacesContext;
88 import javax.servlet.http.HttpServletRequest;
9+import javax.servlet.http.HttpSession;
910
1011 import java.util.logging.Logger;
1112 import javax.inject.Inject;
@@ -25,6 +26,9 @@ import javax.faces.component.html.HtmlCommandLink;
2526
2627 import java.util.Date;
2728 import java.text.SimpleDateFormat;
29+import java.util.UUID;
30+import java.util.Map;
31+import java.util.Hashtable;
2832
2933
3034 @ManagedBean
@@ -40,6 +44,10 @@ public class ChatBean implements Serializable {
4044 //inject user bean
4145 @ManagedProperty("#{userBean}")
4246 private UserBean userBean;
47+
48+ @ManagedProperty("#{pushBean}")
49+ private PushBean pushBean;
50+
4351
4452 private String msg;
4553 private String chatRoom;
@@ -49,6 +57,8 @@ public class ChatBean implements Serializable {
4957
5058 public final static int LIST_LOAD_SIZE = 10;
5159
60+ private String viewId = null;
61+
5262 public void setMsg(String msg) {
5363 this.msg = msg;
5464 }
@@ -70,15 +80,26 @@ public class ChatBean implements Serializable {
7080 //log.info("getLoadLink");
7181 return loadLink;
7282 }
83+ public String getViewId() {
84+ if (viewId == null)
85+ viewId = UUID.randomUUID().toString();
86+ return viewId;
87+ }
88+ public void setViewId(String viewId) {
89+ this.viewId = viewId;
90+ }
7391
7492
75- //userBean injection. setter only.
93+ //session bean injection. setter only.
7694 public void setUserBean(UserBean userBean) {
7795 this.userBean = userBean;
7896 }
97+ public void setPushBean(PushBean pushBean) {
98+ this.pushBean = pushBean;
99+ }
79100
80101 ///chat list data
81- public class ChatMessage {
102+ public static class ChatMessage {
82103 private String uname;
83104 private String msg;
84105 private String date;
@@ -91,10 +112,26 @@ public class ChatBean implements Serializable {
91112 public String getDate() {
92113 return date;
93114 }
115+ public void setUname(String uname) {
116+ this.uname = uname;
117+ }
118+ public void setMsg(String msg) {
119+ this.msg = msg;
120+ }
121+ public void setDate(String date) {
122+ this.date = date;
123+ }
124+ }
125+
126+ public class ViewMsgList {
127+ public String viewId;
128+ public String chatRoom;
129+ public ArrayList<ChatMessage> msgList;
94130 }
95131
96132 private void initMsgList(){
97- log.info("initMsgList: " + chatRoom);
133+ log.info("initMsgList: " + chatRoom + ", msgList: "
134+ + (msgList == null ? "null" : msgList.hashCode()) + ", viewId: " + getViewId());
98135 try {
99136 Connection conn = Resources.getConnection();
100137 String uid = userBean.getUid();
@@ -128,27 +165,44 @@ public class ChatBean implements Serializable {
128165 catch (SQLException se) {
129166 log.severe("sql err!!!");
130167 }
168+
169+ //register viewlist in the session to receive ajax push notification update.
170+ HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
171+ Map<String, ViewMsgList> viewMap = (Map<String, ViewMsgList>) session.getAttribute("viewMap");
172+ if (viewMap == null) {
173+ viewMap = new Hashtable<String, ViewMsgList>();
174+ session.setAttribute("viewMap", viewMap);
175+ }
176+ ViewMsgList mvl = new ViewMsgList();
177+ mvl.viewId = viewId;
178+ mvl.chatRoom = chatRoom;
179+ mvl.msgList = msgList;
180+ viewMap.put(viewId, mvl);
131181 }
132182
133183 public ArrayList<ChatMessage> getMsgList(){
134184 //log.info("getMsgList: " + chatRoom);
135185 return msgList;
136186 }
137-
187+
188+ public static String formatDate() {
189+ Date d = new Date();
190+ SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
191+ return df.format(d);
192+ }
193+
138194 public void doPost() {
139195 //skip empty message....
140196 if (msg.equals(""))
141197 return;
142198
143- Date d = new Date();
144- SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
145-
146199 /*
147200 log.info("user = " + userBean.getUid());
148201 log.info("chat room = " + chatRoom);
149202 log.info("date = " + df.format(d));
150203 */
151204
205+ String dt = formatDate();
152206 try {
153207 Connection conn = Resources.getConnection();
154208 String sqlString = "insert into tb_chat_msg (user_id , chat_room , message , msg_date, deleted) values (?, ?, ?, ?, ?)";
@@ -158,28 +212,32 @@ public class ChatBean implements Serializable {
158212 ps.setString(1, userBean.getUid());
159213 ps.setString(2, chatRoom);
160214 ps.setString(3, msg);
161- ps.setString(4, df.format(d));
215+ ps.setString(4, dt);
162216 ps.setString(5, "false");
163217
164218 int cnt = ps.executeUpdate();
165- log.info(cnt + " records inserted.");
219+ log.info(cnt + " records inserted from the view " + viewId);
166220
167221 conn.close();
168222
223+ /*
224+ no need!!! will be added in jms subscriber onMessage() method.
169225 //add msgList...
170226 ChatMessage m = new ChatMessage();
171227 m.uname = userBean.getUid();
172228 m.msg = msg;
173- m.date = df.format(d);
229+ m.date = dt;
174230 if (msgList == null) {
175231 msgList = new ArrayList<ChatMessage>();
176232 }
177233 msgList.add(m);
234+ */
178235 }
179236 catch (SQLException se) {
180237 log.severe("sql err!!!");
181238 }
182239
240+ pushBean.doPush(viewId, chatRoom, msg, userBean.getUid(), dt);
183241 }
184242
185243 public void loadOldMsg() {
@@ -235,14 +293,14 @@ public class ChatBean implements Serializable {
235293
236294 @PostConstruct
237295 public void postInit() {
238- log.info("postInit");
296+ log.info("postInit @" + this.hashCode());
239297 //log.info("PostConstruct userBean: " + userBean);
240298 //log.info("PostConstruct getFlights: " + userBean.getFlights());
241299 ArrayList<SelectItem> flights = userBean.getFlights();
242300 if (flights != null) {
243301 chatRoom = userBean.getFlights().get(0).getValue().toString();
244302
245- log.info("method: " + ((HttpServletRequest) context.getExternalContext().getRequest()).getMethod());
303+ //log.info("method: " + ((HttpServletRequest) context.getExternalContext().getRequest()).getMethod());
246304 if (((HttpServletRequest) context.getExternalContext().getRequest()).getMethod().equals("GET")) {
247305 log.info("get access. init chat list");
248306 //in get access, the chat room is not selected..
@@ -252,7 +310,7 @@ public class ChatBean implements Serializable {
252310 initLoadLink();
253311 }
254312 }
255-
313+ //log.info("viewId = " + viewId);
256314 }
257315
258316 }
--- a/WEB-INF/classes/motoSample/LoginFilter.java
+++ b/WEB-INF/classes/motoSample/LoginFilter.java
@@ -34,8 +34,8 @@ public class LoginFilter implements Filter {
3434 // Get the userBean from session attribute
3535 UserBean ubean = (UserBean)session.getAttribute("userBean");
3636
37- logger.info("cpath:" + ((HttpServletRequest) request).getContextPath());
38- logger.info("uri:" + ((HttpServletRequest) request).getRequestURI());
37+ //logger.info("cpath:" + ((HttpServletRequest) request).getContextPath());
38+ //logger.info("uri:" + ((HttpServletRequest) request).getRequestURI());
3939
4040 String req_url = ((HttpServletRequest) request).getRequestURI().replace(((HttpServletRequest) request).getContextPath(), "");
4141 logger.info("request_url:" + req_url);
--- a/WEB-INF/classes/motoSample/PushBean.java
+++ b/WEB-INF/classes/motoSample/PushBean.java
@@ -6,6 +6,10 @@ import javax.faces.bean.SessionScoped;
66 import javax.faces.bean.ManagedProperty;
77 import javax.faces.context.FacesContext;
88 import javax.faces.event.ActionEvent;
9+import javax.faces.component.UIComponent;
10+import javax.faces.component.UIInput;
11+
12+import javax.servlet.http.HttpSession;
913
1014 import java.util.logging.Logger;
1115 import javax.inject.Inject;
@@ -18,7 +22,7 @@ import org.richfaces.application.push.MessageException;
1822
1923 import javax.jms.MessageListener;
2024 import javax.jms.Message;
21-import javax.jms.TextMessage;
25+import javax.jms.MapMessage;
2226
2327 import javax.naming.Context;
2428 import javax.naming.InitialContext;
@@ -35,6 +39,10 @@ import javax.jms.TopicSubscriber;
3539 import javax.jms.TopicPublisher;
3640 import javax.jms.JMSException;
3741
42+import java.util.Map;
43+import java.util.Hashtable;
44+import java.util.Enumeration;
45+
3846
3947 @ManagedBean
4048 @SessionScoped
@@ -44,8 +52,9 @@ public class PushBean implements MessageListener, Serializable {
4452 private Logger log;
4553
4654 @Inject
47- private FacesContext context;
55+ private FacesContext context = null;
4856
57+ /*
4958 private int msgCnt = 0;
5059 public void setMsgCnt(int msgCnt) {
5160 this.msgCnt = msgCnt;
@@ -53,6 +62,9 @@ public class PushBean implements MessageListener, Serializable {
5362 public int getMsgCnt() {
5463 return msgCnt;
5564 }
65+ */
66+
67+ private Hashtable<String, ChatBean.ViewMsgList> viewMap;
5668
5769 private static final String PUSH_JMS_TOPIC = "motoTest";
5870
@@ -75,9 +87,9 @@ public class PushBean implements MessageListener, Serializable {
7587 private TopicSubscriber subscriber = null;
7688
7789 @PostConstruct
78- public void initJms() throws JMSException, NamingException {
90+ public void initPushBean() throws JMSException, NamingException {
7991
80- log.info("initJms");
92+ log.info("initPushBean");
8193
8294 if (connection == null) {
8395 TopicConnectionFactory tcf = getTopicConnectionFactory();
@@ -108,31 +120,83 @@ public class PushBean implements MessageListener, Serializable {
108120 // Start the JMS connection; allows messages to be delivered
109121 connection.start( );
110122 log.info("connection start ok.");
111- }
112123
113- public void doPush(ActionEvent event) throws MessageException, JMSException {
114- log.info("push: ");
124+ //get list of receiver view of this session....
125+ HttpSession req_session = (HttpSession) context.getExternalContext().getSession(true);
126+ viewMap = (Hashtable<String, ChatBean.ViewMsgList>) req_session.getAttribute("viewMap");
127+ if (viewMap == null) {
128+ viewMap = new Hashtable<String, ChatBean.ViewMsgList>();
129+ req_session.setAttribute("viewMap", viewMap);
130+ }
131+ }
115132
116- TopicKey topicKey = new TopicKey("flight_no_001");
117- TopicsContext topicsContext = TopicsContext.lookup();
118- topicsContext.publish(topicKey, "empty message");
133+ //this method is invoked from the sender side.
134+ public void doPush(String viewId, String chatRoom, String msg, String uid, String date) {
135+ //UIComponent view = event.getComponent();
136+ //log.info("view: " + view);
137+ //log.info("viewId: " + view.findComponent("viewId"));
119138 /*
139+ String viewId = (String)((UIInput)view.findComponent("viewId")).getValue();
140+ String chatRoom = (String)((UIInput)view.findComponent("chatRoom")).getValue();
141+ String msg = (String)((UIInput)view.findComponent("msg")).getSubmittedValue();
142+ String uid = (String)((UIInput)view.findComponent("uid")).getValue();
120143 */
121-
122- TextMessage message = session.createTextMessage();
123- message.setText("msgggggg");
144+ log.info("push from view " + viewId + ", msg=" + msg);
145+
146+ try {
147+ MapMessage message = session.createMapMessage();
148+ message.setString("viewId", viewId);
149+ message.setString("chatRoom", chatRoom);
150+ message.setString("msg", msg);
151+ message.setString("uid", uid);
152+ message.setString("date", date);
124153 publisher.publish(message);
125154
126- //String uid = userBean.getUid();
127- //log.info("message ");
155+ //deliver jms message
128156 log.info("message published by " + this.hashCode());
157+ //log.info("context: " + context);
158+
159+ //notify ajax.
160+ TopicKey topicKey = new TopicKey(chatRoom);
161+ TopicsContext topicsContext = TopicsContext.lookup();
162+ topicsContext.publish(topicKey, "notify");
163+ /*
164+ */
165+ }
166+ catch (JMSException je) {
167+ log.severe("jms exeption!!");
168+ }
169+ catch (MessageException me) {
170+ log.severe("message exeption!!");
171+ }
129172 }
130173
174+ //this method is invoked on the receiver side.
131175 public void onMessage(Message message) {
132- //String uid = userBean.getUid();
133- msgCnt++;
176+ //msgCnt++;
134177 log.info("message received by " + this.hashCode());
135-
178+ //log.info("viewMap size: " + viewMap.size());
179+ MapMessage mmsg = (MapMessage)message;
180+
181+ for (Enumeration<ChatBean.ViewMsgList> em = viewMap.elements(); em.hasMoreElements(); ) {
182+ ChatBean.ViewMsgList vml = em.nextElement();
183+ //update the view only if it has the same chat room
184+ try {
185+ if (!mmsg.getString("chatRoom").equals(vml.chatRoom))
186+ continue;
187+
188+ ChatBean.ChatMessage m = new ChatBean.ChatMessage();
189+ m.setUname(mmsg.getString("uid"));
190+ m.setMsg(mmsg.getString("msg"));
191+ m.setDate(mmsg.getString("date"));
192+ vml.msgList.add(m);
193+
194+ log.info("update msgview list!!: " + vml.viewId);
195+ }
196+ catch (JMSException je) {
197+ log.severe("get message error!!");
198+ }
199+ }
136200 }
137201
138202 @PreDestroy
--- a/secured/chat.xhtml
+++ b/secured/chat.xhtml
@@ -11,17 +11,20 @@
1111
1212 user : #{userBean.uname} <br />
1313
14+ <h:outputText value="#{chatBean.viewId}"/>
15+ <br />
16+
1417 <h:form xmlns:a4j="http://richfaces.org/a4j"
1518 xmlns:rich="http://richfaces.org/rich">
1619
20+ <h:inputHidden id="viewId" value="#{chatBean.viewId}"/>
21+
1722 <a4j:push address="#{chatBean.chatRoom}">
18- <!--a4j:push address="motoTest"-->
19- <a4j:ajax event="dataavailable" render="msgList msgCnt" />
23+ <a4j:ajax event="dataavailable" render="chatTable" />
2024 </a4j:push>
21- <h:outputText id="msgCnt" value="#{pushBean.msgCnt}"/>
22-
25+
2326 chat room :
24- <h:selectOneMenu id="charRoom" value="#{chatBean.chatRoom}" onchange="submit()"
27+ <h:selectOneMenu id="chatRoom" value="#{chatBean.chatRoom}" onchange="submit()"
2528 valueChangeListener="#{chatBean.chatRoomChanged}" >
2629 <f:selectItems value="#{userBean.flights}"/>
2730 </h:selectOneMenu>
@@ -59,8 +62,8 @@
5962 input text: <h:inputText id="msg" value="#{chatBean.msg}"/>
6063 </h:panelGroup>
6164
62- <h:commandButton value="post" action="#{chatBean.doPost}" actionListener="#{pushBean.doPush}" >
63- <f:ajax execute="charRoom msg" render="chatTable" />
65+ <h:commandButton value="post" action="#{chatBean.doPost}" >
66+ <f:ajax execute="viewId chatRoom msg" render="chatTable" />
6467 </h:commandButton>
6568 </h:form>
6669