Author: nbelaevski
Date: 2010-10-12 12:55:58 -0400 (Tue, 12 Oct 2010)
New Revision: 19546
Added:
branches/RF-7817/push-redesign-app/src/main/java/demo/ChatBean.java
Removed:
branches/RF-7817/push-redesign-app/src/main/java/demo/Bean.java
Modified:
branches/RF-7817/push-redesign-app/src/main/webapp/index.xhtml
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionImpl.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionTrackerImpl.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionsWorker.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/RequestImpl.java
Log:
https://jira.jboss.org/browse/RF-7817:
- bug fixing
- chat demo improved
Modified:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionImpl.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionImpl.java 2010-10-12
15:44:25 UTC (rev 19545)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionImpl.java 2010-10-12
16:55:58 UTC (rev 19546)
@@ -141,7 +141,7 @@
request.resume();
}
- request.removeListener(this);
+ //TODO - request.removeListener(this) ?
resetExpirationTime();
pushTracker.onRequestDisconnected(this);
}
@@ -167,6 +167,7 @@
for (TopicKey topicKey : topics) {
subscriptionContext.removeMessageListener(topicKey, this);
}
+ pushTracker.getPushSessionsWorker().removeAll(this);
}
public void writeMessages() throws IOException {
Modified:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionTrackerImpl.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionTrackerImpl.java 2010-10-12
15:44:25 UTC (rev 19545)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionTrackerImpl.java 2010-10-12
16:55:58 UTC (rev 19546)
@@ -24,7 +24,6 @@
import java.util.UUID;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.DelayQueue;
-import java.util.concurrent.Executors;
import org.richfaces.application.push.PushSessionTracker;
import org.richfaces.application.push.SubscriptionContext;
@@ -63,7 +62,9 @@
public PushSessionTrackerImpl(SubscriptionContext subscriptionContext) {
//TODO use configurable executor service
- Executors.newFixedThreadPool(1).submit(new SessionsExpirationRunnable());
+ Thread t = new Thread(new SessionsExpirationRunnable(),
"rf-push-session-tracker");
+ t.setDaemon(true);
+ t.start();
this.subscriptionContext = subscriptionContext;
this.pushSessionsWorker = new PushSessionsWorker(8);
Modified:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionsWorker.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionsWorker.java 2010-10-12
15:44:25 UTC (rev 19545)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionsWorker.java 2010-10-12
16:55:58 UTC (rev 19546)
@@ -37,10 +37,18 @@
private BlockingQueue<PushSession> sessionsQueue = new
LinkedBlockingQueue<PushSession>();
+ //TODO - number of sessions posted to the queue should not exceed two items
public void submit(PushSession session) {
sessionsQueue.add(session);
}
+ public void removeAll(PushSession session) {
+ boolean elementRemoved;
+ do {
+ elementRemoved = sessionsQueue.remove(session);
+ } while (elementRemoved);
+ }
+
public void run() {
while (true) {
try {
@@ -90,7 +98,9 @@
pipelines = new Pipeline[numThreads];
for (int i = 0; i < pipelines.length; i++) {
pipelines[i] = new Pipeline();
- new Thread(pipelines[i]).start();
+ Thread t = new Thread(pipelines[i], "rf-push-broadcater-" + i);
+ t.setDaemon(true);
+ t.start();
}
}
@@ -98,4 +108,9 @@
int pipelineIdx = hashIdx(session.getId());
pipelines[pipelineIdx].submit(session);
}
+
+ public void removeAll(PushSession session) {
+ int pipelineIdx = hashIdx(session.getId());
+ pipelines[pipelineIdx].removeAll(session);
+ }
}
Modified:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/RequestImpl.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/RequestImpl.java 2010-10-12
15:44:25 UTC (rev 19545)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/RequestImpl.java 2010-10-12
16:55:58 UTC (rev 19546)
@@ -23,8 +23,8 @@
import java.io.IOException;
import java.io.PrintWriter;
+import java.util.ArrayList;
import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -52,8 +52,7 @@
private AtmosphereResource<HttpServletRequest, HttpServletResponse>
atmosphereResource;
- //TODO - performance?
- private List<RequestLifecycleListener> listeners = new
CopyOnWriteArrayList<RequestLifecycleListener>();
+ private List<RequestLifecycleListener> listeners = new
ArrayList<RequestLifecycleListener>();
private AtmosphereResourceEventListener atmosphereListener = new
AtmosphereResourceEventListener() {
Deleted: branches/RF-7817/push-redesign-app/src/main/java/demo/Bean.java
===================================================================
--- branches/RF-7817/push-redesign-app/src/main/java/demo/Bean.java 2010-10-12 15:44:25
UTC (rev 19545)
+++ branches/RF-7817/push-redesign-app/src/main/java/demo/Bean.java 2010-10-12 16:55:58
UTC (rev 19546)
@@ -1,62 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package demo;
-
-import javax.faces.bean.ManagedBean;
-import javax.faces.bean.ManagedProperty;
-import javax.faces.bean.RequestScoped;
-
-import org.richfaces.application.push.PublisherContext;
-import org.richfaces.application.push.TopicKey;
-
-/**
- * @author Nick Belaevski
- *
- */
-@ManagedBean
-@RequestScoped
-public class Bean {
-
- private String message;
-
- @ManagedProperty("#{" + PublisherContext.ATTRIBUTE_NAME + "}")
- private PublisherContext publisherContext;
-
- public String getMessage() {
- return message;
- }
-
- public void setMessage(String message) {
- this.message = message;
- }
-
- public void say() {
- publisherContext.publish(new TopicKey("chat"), message);
- }
-
- /**
- * @param publisherContext the publisherContext to set
- */
- public void setPublisherContext(PublisherContext publisherContext) {
- this.publisherContext = publisherContext;
- }
-}
Copied: branches/RF-7817/push-redesign-app/src/main/java/demo/ChatBean.java (from rev
19527, branches/RF-7817/push-redesign-app/src/main/java/demo/Bean.java)
===================================================================
--- branches/RF-7817/push-redesign-app/src/main/java/demo/ChatBean.java
(rev 0)
+++ branches/RF-7817/push-redesign-app/src/main/java/demo/ChatBean.java 2010-10-12
16:55:58 UTC (rev 19546)
@@ -0,0 +1,93 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package demo;
+
+import java.io.Serializable;
+import java.text.MessageFormat;
+import java.util.Date;
+
+import javax.faces.bean.ManagedBean;
+import javax.faces.bean.ManagedProperty;
+import javax.faces.bean.ViewScoped;
+
+import org.richfaces.application.push.PublisherContext;
+import org.richfaces.application.push.TopicKey;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+@ManagedBean
+@ViewScoped
+public class ChatBean implements Serializable {
+
+ private static final long serialVersionUID = -6377543444437645751L;
+
+ private String userName;
+
+ private String message;
+
+ private boolean chatJoined;
+
+ @ManagedProperty("#{" + PublisherContext.ATTRIBUTE_NAME + "}")
+ private transient PublisherContext publisherContext;
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+
+ public void joinChat() {
+ if (!chatJoined) {
+ if (userName == null) {
+ throw new NullPointerException("username");
+ }
+
+ chatJoined = true;
+ publisherContext.publish(new TopicKey("chat"),
MessageFormat.format("*** {0} joined chat in {1,time,medium}",
+ userName, new Date()));
+ }
+ }
+
+ public void say() {
+ publisherContext.publish(new TopicKey("chat"),
MessageFormat.format("{0,time,medium} {1}: {2}", new Date(),
+ userName, message));
+ }
+
+ /**
+ * @param publisherContext the publisherContext to set
+ */
+ public void setPublisherContext(PublisherContext publisherContext) {
+ this.publisherContext = publisherContext;
+ }
+}
Modified: branches/RF-7817/push-redesign-app/src/main/webapp/index.xhtml
===================================================================
--- branches/RF-7817/push-redesign-app/src/main/webapp/index.xhtml 2010-10-12 15:44:25 UTC
(rev 19545)
+++ branches/RF-7817/push-redesign-app/src/main/webapp/index.xhtml 2010-10-12 16:55:58 UTC
(rev 19546)
@@ -7,19 +7,35 @@
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://richfaces.org/push-redesign">
- <h:head>
- </h:head>
- <h:body>
- <p:push topic="chat" ondataavailable="jQuery('<div
/>').appendTo(jQuery('#group')).text(data)" />
+ <f:view>
+ <f:metadata>
+ <f:viewParam name="username" value="#{chatBean.userName}"
/>
+ <f:event type="preRenderView"
listener="#{chatBean.joinChat}"/>
+ </f:metadata>
+ <h:head>
+ </h:head>
+ <h:body>
+ <h:form id="form">
+ <p:push topic="chat" ondataavailable="jQuery('<div
/>').prependTo('.chatOutput').text(data)" />
- <h:form>
- <h:inputText value="#{bean.message}" />
-
- <h:commandLink value="ajax" action="#{bean.say}">
- <f:ajax execute="@form" />
- </h:commandLink>
- </h:form>
-
- <h:panelGroup id="group"
layout="block"></h:panelGroup>
- </h:body>
+ You are: #{chatBean.userName} <br />
+
+ <h:inputText styleClass="messageInput"
value="#{chatBean.message}" size="40" />
+
+ <script type="text/javascript">
+ function clearInput(event) {
+ if (event.status == 'success') {
+ jQuery('.messageInput').val('');
+ }
+ }
+ </script>
+
+ <h:commandLink value="ajax" action="#{chatBean.say}">
+ <f:ajax execute="@form" onevent="clearInput" />
+ </h:commandLink>
+
+ <h:panelGroup id="group" styleClass="chatOutput"
layout="block" />
+ </h:form>
+ </h:body>
+ </f:view>
</html>
\ No newline at end of file