/[projects]/android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java
ViewVC logotype

Contents of /android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 762 - (show annotations) (download)
Thu May 27 11:30:16 2010 UTC (13 years, 11 months ago) by torben
File size: 4271 byte(s)
Nice shutdown of memcache client
1 package dk.thoerup.marketstats;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.net.InetSocketAddress;
6 import java.util.ArrayList;
7 import java.util.Date;
8 import java.util.Locale;
9 import java.util.concurrent.TimeUnit;
10 import java.util.logging.Logger;
11
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import net.spy.memcached.MemcachedClient;
18
19 import com.gc.android.market.api.MarketSession;
20 import com.gc.android.market.api.model.Market.AppsRequest;
21 import com.gc.android.market.api.model.Market.CommentsRequest;
22
23
24
25 public class ShowStats extends HttpServlet {
26 private static final long serialVersionUID = 1L;
27
28 final int TIMEOUT = 30*60;
29 static final Logger log = Logger.getLogger(ShowStats.class.getName());
30
31 String login;
32 String password;
33
34 MemcachedClient memcache = null;
35
36
37 @Override
38 public void init() throws ServletException {
39 super.init();
40
41 login = getServletContext().getInitParameter("login");
42 password = getServletContext().getInitParameter("password");
43
44 try {
45 memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211));
46 } catch (IOException e) {
47 throw new ServletException(e);
48 }
49 }
50
51 @Override
52 public void destroy() {
53 super.destroy();
54
55 memcache.shutdown(3, TimeUnit.SECONDS);
56 memcache = null;
57 }
58
59 protected String doLookup(String query) throws IOException {
60
61
62 String key = "marketstats:" + query.replace(' ', '_');
63 String response = (String) memcache.get(key);
64
65 if (response == null) {
66 response = doLookupWorker(query);
67 memcache.set(key, TIMEOUT, response);
68 response += "<!-- new lookup -->";
69 } else {
70 response += "<!-- from memcached -->";
71 }
72 return response;
73 }
74
75 private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) {
76 CommentCallback commentsCb = new CommentCallback();
77 commentsCb.setList( commentBeans );
78
79 session.setLocale( Locale.ROOT );
80
81
82
83 int start = 0;
84 do {
85 int count = 10;
86 if (start > 0)
87 count = Math.min(10, commentsCb.getEntryCount() );
88
89 //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
90 CommentsRequest commentsRequest = CommentsRequest.newBuilder()
91 .setAppId(appId)
92 .setStartIndex(start)
93 .setEntriesCount(count)
94 .build();
95
96 session.append(commentsRequest, commentsCb);
97 session.flush();
98 start +=10;
99
100 if (start >= 200)
101 break; //emergency brake
102
103 } while ( start < commentsCb.getEntryCount() );
104 }
105
106
107 protected String doLookupWorker(String query) {
108 final StringBuilder sb = new StringBuilder();
109
110
111 MarketSession session = new MarketSession();
112 session.login(login,password);
113
114
115 AppsRequest appsRequest = AppsRequest.newBuilder()
116 .setQuery(query)
117 .setStartIndex(0).setEntriesCount(10)
118 .setWithExtendedInfo(true)
119 .build();
120
121
122 AppsCallback appsCb = new AppsCallback() ;
123 appsCb.setStringBuffer(sb);
124
125 ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
126
127
128
129 session.append(appsRequest, appsCb);
130 session.flush();
131
132 String appId = appsCb.getAppId();
133
134 if (appId != null) {
135
136 loadComments(appId, session, commentBeans);
137
138 sb.append("-----------------------------------------------------------------\n");
139 for (CommentBean c : commentBeans) {
140 sb.append("User: " + c.author + "\n");
141 sb.append("Rating: " + c.rating + "\n");
142 sb.append("Time: " + new Date(c.time).toString() + "\n");
143 sb.append( c.text + "\n");
144 sb.append("\n");
145
146 }
147 sb.append("Comments: " + commentBeans.size() + "\n");
148 }
149 sb.append("<!--Cache.Timeout=" + TIMEOUT/60 + " minutes-->\n");
150
151
152 return sb.toString();
153 }
154
155 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
156 String query = request.getParameter("query");
157
158 response.setContentType("text/html");
159 PrintWriter out = response.getWriter();
160 out.print( "<html><body><pre>" + doLookup(query) + "</pre></body></html>" );
161 }
162
163 }

  ViewVC Help
Powered by ViewVC 1.1.20