PushService.java
资源名称:WAPpush.zip [点击查看]
上传用户:weisa_1
上传日期:2007-10-14
资源大小:287k
文件大小:3k
源码类别:
手机WAP编程
开发平台:
Java
- package push;
- import members.Members;
- import java.util.Vector;
- import java.util.StringTokenizer;
- /**
- * Handling of user selected push services
- *
- * @version 1.0
- * @since 1.0
- * @see Runnable
- */
- public final class PushService implements Runnable {
- private final PushInitiator pusher;
- private final String serviceContextPath;
- /**
- * interval in hours between Push message submissions
- */
- private final int interval;
- private final String addressType;
- public static final String SI_CONTENT_TYPE = "text/vnd.wap.si";
- /**
- * Creates a new <code>PushService</code> instance.
- *
- * @param pusher a <code>PushInitiator</code> value
- * @param serviceContextPath a <code>String</code> value
- * @param interval an <code>int</code> value; in hours
- */
- public PushService(PushInitiator pusher, String serviceContextPath, String addressType, int interval) {
- this.pusher = pusher;
- this.serviceContextPath = serviceContextPath;
- this.interval = interval;
- this.addressType = addressType;
- }
- /**
- * A push service checker thread sends push messages related to ordered services
- */
- public void run() {
- while(true){
- try {
- Vector users = User.getUsersOnline();
- for(int i=0; i<users.size(); i++) {
- User user = (User)users.get(i);
- String name = user.getName();
- String pushServices = user.getPushServices();
- /* Check if the user has selected push services. If so, then generate
- and send (if not yet sent within a time interval) a push message! */
- if (pushServices != null){
- StringTokenizer st = new StringTokenizer(pushServices, ";");
- while (st.hasMoreTokens()){
- int serviceId = Integer.parseInt(st.nextToken());
- Object pushService = Members.getPushService(serviceId);
- String serviceName = ((String[])pushService)[0];
- String serviceUrl = ((String[])pushService)[1];
- Object serviceObject = user.getProperty(serviceName);
- if(serviceObject != null) {
- if((System.currentTimeMillis() - ((Long)serviceObject).longValue()) >
- interval*1000*60*60)
- /* If there are more hours than specified by the interval variable
- from the previous submission, the service message is ready to
- be sent again.*/
- serviceObject = null;
- }
- if (serviceObject == null) {
- String url = serviceContextPath + serviceUrl;
- String message = PushInitiator.createSiMessage(url, null, null, null,
- null, serviceName);
- String address = user.getAddress(addressType);
- System.out.println(message);
- System.out.println("client Address = " + address);
- if(address != null && !address.trim().equals("")) {
- try {
- pusher.sendPushMessage(address, addressType, message, SI_CONTENT_TYPE);
- user.setProperty(serviceName, new Long(System.currentTimeMillis()));
- }
- catch(Exception ee) {
- System.err.println("Error in sending push message!");
- }
- }
- }
- }
- }
- }
- }
- catch(Exception e) { e.printStackTrace(); }
- try {
- Thread.sleep(10000); // 10 s
- } catch(InterruptedException e){}
- }
- }
- }