TaskExecuteService

package cn.focus.dc.app.xinfang.service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

@Service
public class TaskExecuteService {
    
    private static final int FIX_THREAD_COUNT = 10;
    
    private ExecutorService executor;

    @PostConstruct
    public void init() {
        executor = Executors.newFixedThreadPool(FIX_THREAD_COUNT);
    }
    
    public void execute(Runnable command) {
        executor.execute(command);
    }
}



    /**
     * 向用户推送关注楼盘的爬房信息, 异步推送
     * @param uids
     * @param buildingName 楼盘名
     */
    public void push4FavoriteBuilding(final List<Integer> uids, final int buildId, final String buildName) {
        //构造异步推送支持
        taskExecuteService.execute(new Runnable() {
            
            @Override
            public void run() {
                //构造推送消息
                PushMessage pushMessage= new PushMessage();
                pushMessage.setMessage(String.format(AppConstants.HouseLookingConst.FAVORITE_PUSH_INFO, 
                        buildName));
                pushMessage.setTitle(AppConstants.PushConst.WARM_TIPS);
                pushMessage.setType(PushType.FAVORITE_HOUSE_LOOKING_GROUP);
                pushMessage.setId(buildId);
                
                //分批推送
                int count = 300;
                int fromIndex = 0;
                int toIndex = 0;
                while (fromIndex < uids.size()) {
                    toIndex = fromIndex + count;
                    toIndex = toIndex > uids.size() ? uids.size() : toIndex;
                    List<Integer> uidList = uids.subList(fromIndex, toIndex);
                    fromIndex = toIndex;
                    
                    //android推送,修改推送接口支持批量推送
                    List<Integer> androidUids = userPushAndroidService.filterByVersion(uidList, VERSION); 
                    pushMessage.setUids(androidUids);
                    androidPushService.pushMessage(pushMessage);

                    //iOS推送
                    List<UserPush> userPushs = userPushService.getUserPushByVersion(uidList, VERSION);
                    List<Integer> iOSUids = StripUtils.stripIntList(userPushs, PushUtils.USER_PUST_UID_STRIPPTER);
                    List<String> tokens = StripUtils.stripStrList(userPushs, PushUtils.USER_PUSH_TOKEN_STRIPPER); 
                    pushMessage.setPushTokens(tokens);
                    iOSApnsSendService.pushMessage(pushMessage);
                    
                    //保存信息箱动态
                    saveNews(androidUids, iOSUids, pushMessage);
                }

            }
        });
    }

你可能感兴趣的:(service)