moto web application
修订版 | 9cff5fc9af03e5a87470e20aa16a25a46fb0ddd8 (tree) |
---|---|
时间 | 2014-01-24 16:45:10 |
作者 | astoria-d <astoria-d@mail...> |
Commiter | astoria-d |
inter browsers ajax push notification updated.
@@ -4,3 +4,4 @@ deleted/* | ||
4 | 4 | this-proj-is-jsf2.0-named-annotation.txt |
5 | 5 | data-source-memo.txt |
6 | 6 | aaa.txt |
7 | +jboss.cmd-shortcut.lnk |
@@ -6,6 +6,7 @@ import javax.faces.bean.ViewScoped; | ||
6 | 6 | import javax.faces.bean.ManagedProperty; |
7 | 7 | import javax.faces.context.FacesContext; |
8 | 8 | import javax.servlet.http.HttpServletRequest; |
9 | +import javax.servlet.http.HttpSession; | |
9 | 10 | |
10 | 11 | import java.util.logging.Logger; |
11 | 12 | import javax.inject.Inject; |
@@ -25,6 +26,9 @@ import javax.faces.component.html.HtmlCommandLink; | ||
25 | 26 | |
26 | 27 | import java.util.Date; |
27 | 28 | import java.text.SimpleDateFormat; |
29 | +import java.util.UUID; | |
30 | +import java.util.Map; | |
31 | +import java.util.Hashtable; | |
28 | 32 | |
29 | 33 | |
30 | 34 | @ManagedBean |
@@ -40,6 +44,10 @@ public class ChatBean implements Serializable { | ||
40 | 44 | //inject user bean |
41 | 45 | @ManagedProperty("#{userBean}") |
42 | 46 | private UserBean userBean; |
47 | + | |
48 | + @ManagedProperty("#{pushBean}") | |
49 | + private PushBean pushBean; | |
50 | + | |
43 | 51 | |
44 | 52 | private String msg; |
45 | 53 | private String chatRoom; |
@@ -49,6 +57,8 @@ public class ChatBean implements Serializable { | ||
49 | 57 | |
50 | 58 | public final static int LIST_LOAD_SIZE = 10; |
51 | 59 | |
60 | + private String viewId = null; | |
61 | + | |
52 | 62 | public void setMsg(String msg) { |
53 | 63 | this.msg = msg; |
54 | 64 | } |
@@ -70,15 +80,26 @@ public class ChatBean implements Serializable { | ||
70 | 80 | //log.info("getLoadLink"); |
71 | 81 | return loadLink; |
72 | 82 | } |
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 | + } | |
73 | 91 | |
74 | 92 | |
75 | - //userBean injection. setter only. | |
93 | + //session bean injection. setter only. | |
76 | 94 | public void setUserBean(UserBean userBean) { |
77 | 95 | this.userBean = userBean; |
78 | 96 | } |
97 | + public void setPushBean(PushBean pushBean) { | |
98 | + this.pushBean = pushBean; | |
99 | + } | |
79 | 100 | |
80 | 101 | ///chat list data |
81 | - public class ChatMessage { | |
102 | + public static class ChatMessage { | |
82 | 103 | private String uname; |
83 | 104 | private String msg; |
84 | 105 | private String date; |
@@ -91,10 +112,26 @@ public class ChatBean implements Serializable { | ||
91 | 112 | public String getDate() { |
92 | 113 | return date; |
93 | 114 | } |
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; | |
94 | 130 | } |
95 | 131 | |
96 | 132 | private void initMsgList(){ |
97 | - log.info("initMsgList: " + chatRoom); | |
133 | + log.info("initMsgList: " + chatRoom + ", msgList: " | |
134 | + + (msgList == null ? "null" : msgList.hashCode()) + ", viewId: " + getViewId()); | |
98 | 135 | try { |
99 | 136 | Connection conn = Resources.getConnection(); |
100 | 137 | String uid = userBean.getUid(); |
@@ -128,27 +165,44 @@ public class ChatBean implements Serializable { | ||
128 | 165 | catch (SQLException se) { |
129 | 166 | log.severe("sql err!!!"); |
130 | 167 | } |
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); | |
131 | 181 | } |
132 | 182 | |
133 | 183 | public ArrayList<ChatMessage> getMsgList(){ |
134 | 184 | //log.info("getMsgList: " + chatRoom); |
135 | 185 | return msgList; |
136 | 186 | } |
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 | + | |
138 | 194 | public void doPost() { |
139 | 195 | //skip empty message.... |
140 | 196 | if (msg.equals("")) |
141 | 197 | return; |
142 | 198 | |
143 | - Date d = new Date(); | |
144 | - SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); | |
145 | - | |
146 | 199 | /* |
147 | 200 | log.info("user = " + userBean.getUid()); |
148 | 201 | log.info("chat room = " + chatRoom); |
149 | 202 | log.info("date = " + df.format(d)); |
150 | 203 | */ |
151 | 204 | |
205 | + String dt = formatDate(); | |
152 | 206 | try { |
153 | 207 | Connection conn = Resources.getConnection(); |
154 | 208 | 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 { | ||
158 | 212 | ps.setString(1, userBean.getUid()); |
159 | 213 | ps.setString(2, chatRoom); |
160 | 214 | ps.setString(3, msg); |
161 | - ps.setString(4, df.format(d)); | |
215 | + ps.setString(4, dt); | |
162 | 216 | ps.setString(5, "false"); |
163 | 217 | |
164 | 218 | int cnt = ps.executeUpdate(); |
165 | - log.info(cnt + " records inserted."); | |
219 | + log.info(cnt + " records inserted from the view " + viewId); | |
166 | 220 | |
167 | 221 | conn.close(); |
168 | 222 | |
223 | + /* | |
224 | + no need!!! will be added in jms subscriber onMessage() method. | |
169 | 225 | //add msgList... |
170 | 226 | ChatMessage m = new ChatMessage(); |
171 | 227 | m.uname = userBean.getUid(); |
172 | 228 | m.msg = msg; |
173 | - m.date = df.format(d); | |
229 | + m.date = dt; | |
174 | 230 | if (msgList == null) { |
175 | 231 | msgList = new ArrayList<ChatMessage>(); |
176 | 232 | } |
177 | 233 | msgList.add(m); |
234 | + */ | |
178 | 235 | } |
179 | 236 | catch (SQLException se) { |
180 | 237 | log.severe("sql err!!!"); |
181 | 238 | } |
182 | 239 | |
240 | + pushBean.doPush(viewId, chatRoom, msg, userBean.getUid(), dt); | |
183 | 241 | } |
184 | 242 | |
185 | 243 | public void loadOldMsg() { |
@@ -235,14 +293,14 @@ public class ChatBean implements Serializable { | ||
235 | 293 | |
236 | 294 | @PostConstruct |
237 | 295 | public void postInit() { |
238 | - log.info("postInit"); | |
296 | + log.info("postInit @" + this.hashCode()); | |
239 | 297 | //log.info("PostConstruct userBean: " + userBean); |
240 | 298 | //log.info("PostConstruct getFlights: " + userBean.getFlights()); |
241 | 299 | ArrayList<SelectItem> flights = userBean.getFlights(); |
242 | 300 | if (flights != null) { |
243 | 301 | chatRoom = userBean.getFlights().get(0).getValue().toString(); |
244 | 302 | |
245 | - log.info("method: " + ((HttpServletRequest) context.getExternalContext().getRequest()).getMethod()); | |
303 | + //log.info("method: " + ((HttpServletRequest) context.getExternalContext().getRequest()).getMethod()); | |
246 | 304 | if (((HttpServletRequest) context.getExternalContext().getRequest()).getMethod().equals("GET")) { |
247 | 305 | log.info("get access. init chat list"); |
248 | 306 | //in get access, the chat room is not selected.. |
@@ -252,7 +310,7 @@ public class ChatBean implements Serializable { | ||
252 | 310 | initLoadLink(); |
253 | 311 | } |
254 | 312 | } |
255 | - | |
313 | + //log.info("viewId = " + viewId); | |
256 | 314 | } |
257 | 315 | |
258 | 316 | } |
@@ -34,8 +34,8 @@ public class LoginFilter implements Filter { | ||
34 | 34 | // Get the userBean from session attribute |
35 | 35 | UserBean ubean = (UserBean)session.getAttribute("userBean"); |
36 | 36 | |
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()); | |
39 | 39 | |
40 | 40 | String req_url = ((HttpServletRequest) request).getRequestURI().replace(((HttpServletRequest) request).getContextPath(), ""); |
41 | 41 | logger.info("request_url:" + req_url); |
@@ -6,6 +6,10 @@ import javax.faces.bean.SessionScoped; | ||
6 | 6 | import javax.faces.bean.ManagedProperty; |
7 | 7 | import javax.faces.context.FacesContext; |
8 | 8 | import javax.faces.event.ActionEvent; |
9 | +import javax.faces.component.UIComponent; | |
10 | +import javax.faces.component.UIInput; | |
11 | + | |
12 | +import javax.servlet.http.HttpSession; | |
9 | 13 | |
10 | 14 | import java.util.logging.Logger; |
11 | 15 | import javax.inject.Inject; |
@@ -18,7 +22,7 @@ import org.richfaces.application.push.MessageException; | ||
18 | 22 | |
19 | 23 | import javax.jms.MessageListener; |
20 | 24 | import javax.jms.Message; |
21 | -import javax.jms.TextMessage; | |
25 | +import javax.jms.MapMessage; | |
22 | 26 | |
23 | 27 | import javax.naming.Context; |
24 | 28 | import javax.naming.InitialContext; |
@@ -35,6 +39,10 @@ import javax.jms.TopicSubscriber; | ||
35 | 39 | import javax.jms.TopicPublisher; |
36 | 40 | import javax.jms.JMSException; |
37 | 41 | |
42 | +import java.util.Map; | |
43 | +import java.util.Hashtable; | |
44 | +import java.util.Enumeration; | |
45 | + | |
38 | 46 | |
39 | 47 | @ManagedBean |
40 | 48 | @SessionScoped |
@@ -44,8 +52,9 @@ public class PushBean implements MessageListener, Serializable { | ||
44 | 52 | private Logger log; |
45 | 53 | |
46 | 54 | @Inject |
47 | - private FacesContext context; | |
55 | + private FacesContext context = null; | |
48 | 56 | |
57 | + /* | |
49 | 58 | private int msgCnt = 0; |
50 | 59 | public void setMsgCnt(int msgCnt) { |
51 | 60 | this.msgCnt = msgCnt; |
@@ -53,6 +62,9 @@ public class PushBean implements MessageListener, Serializable { | ||
53 | 62 | public int getMsgCnt() { |
54 | 63 | return msgCnt; |
55 | 64 | } |
65 | + */ | |
66 | + | |
67 | + private Hashtable<String, ChatBean.ViewMsgList> viewMap; | |
56 | 68 | |
57 | 69 | private static final String PUSH_JMS_TOPIC = "motoTest"; |
58 | 70 |
@@ -75,9 +87,9 @@ public class PushBean implements MessageListener, Serializable { | ||
75 | 87 | private TopicSubscriber subscriber = null; |
76 | 88 | |
77 | 89 | @PostConstruct |
78 | - public void initJms() throws JMSException, NamingException { | |
90 | + public void initPushBean() throws JMSException, NamingException { | |
79 | 91 | |
80 | - log.info("initJms"); | |
92 | + log.info("initPushBean"); | |
81 | 93 | |
82 | 94 | if (connection == null) { |
83 | 95 | TopicConnectionFactory tcf = getTopicConnectionFactory(); |
@@ -108,31 +120,83 @@ public class PushBean implements MessageListener, Serializable { | ||
108 | 120 | // Start the JMS connection; allows messages to be delivered |
109 | 121 | connection.start( ); |
110 | 122 | log.info("connection start ok."); |
111 | - } | |
112 | 123 | |
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 | + } | |
115 | 132 | |
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")); | |
119 | 138 | /* |
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(); | |
120 | 143 | */ |
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); | |
124 | 153 | publisher.publish(message); |
125 | 154 | |
126 | - //String uid = userBean.getUid(); | |
127 | - //log.info("message "); | |
155 | + //deliver jms message | |
128 | 156 | 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 | + } | |
129 | 172 | } |
130 | 173 | |
174 | + //this method is invoked on the receiver side. | |
131 | 175 | public void onMessage(Message message) { |
132 | - //String uid = userBean.getUid(); | |
133 | - msgCnt++; | |
176 | + //msgCnt++; | |
134 | 177 | 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 | + } | |
136 | 200 | } |
137 | 201 | |
138 | 202 | @PreDestroy |
@@ -11,17 +11,20 @@ | ||
11 | 11 | |
12 | 12 | user : #{userBean.uname} <br /> |
13 | 13 | |
14 | + <h:outputText value="#{chatBean.viewId}"/> | |
15 | + <br /> | |
16 | + | |
14 | 17 | <h:form xmlns:a4j="http://richfaces.org/a4j" |
15 | 18 | xmlns:rich="http://richfaces.org/rich"> |
16 | 19 | |
20 | + <h:inputHidden id="viewId" value="#{chatBean.viewId}"/> | |
21 | + | |
17 | 22 | <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" /> | |
20 | 24 | </a4j:push> |
21 | - <h:outputText id="msgCnt" value="#{pushBean.msgCnt}"/> | |
22 | - | |
25 | + | |
23 | 26 | chat room : |
24 | - <h:selectOneMenu id="charRoom" value="#{chatBean.chatRoom}" onchange="submit()" | |
27 | + <h:selectOneMenu id="chatRoom" value="#{chatBean.chatRoom}" onchange="submit()" | |
25 | 28 | valueChangeListener="#{chatBean.chatRoomChanged}" > |
26 | 29 | <f:selectItems value="#{userBean.flights}"/> |
27 | 30 | </h:selectOneMenu> |
@@ -59,8 +62,8 @@ | ||
59 | 62 | input text: <h:inputText id="msg" value="#{chatBean.msg}"/> |
60 | 63 | </h:panelGroup> |
61 | 64 | |
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" /> | |
64 | 67 | </h:commandButton> |
65 | 68 | </h:form> |
66 | 69 |