Browse Source

将广告主信息放到内存中

jun.zhou 9 years ago
parent
commit
d9a115afe3

+ 46 - 0
src/main/java/com/cloudcross/ssp/listener/ApplicationListener.java

@@ -0,0 +1,46 @@
+package com.cloudcross.ssp.listener;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import org.apache.log4j.Logger;
+import org.springframework.context.ApplicationContext;
+import org.springframework.web.context.support.WebApplicationContextUtils;
+
+import com.cloudcross.ssp.base.dao.GenericIBatisDao;
+import com.cloudcross.ssp.loader.AdvertiserLoader;
+import com.cloudcross.ssp.model.Advertiser;
+
+
+public class ApplicationListener implements ServletContextListener{
+	private static final Logger log = Logger.getLogger(ApplicationListener.class);
+	
+	@Override
+	public void contextDestroyed(ServletContextEvent sce) {
+		// TODO Auto-generated method stub
+		
+	}
+	
+	@Override
+	public void contextInitialized(ServletContextEvent sce) {
+		ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
+		GenericIBatisDao myBatisDao = (GenericIBatisDao) context.getBean("iBatisDao");
+		log.debug("开始将广告主信息加载到内存");
+		initAdvertiser(myBatisDao);//初始化广告主代理商信息
+		log.debug("广告主信息加载完毕");
+	}
+	
+	public void initAdvertiser(GenericIBatisDao myBatisDao){
+		List<Advertiser> list = myBatisDao.getList("advertiserSqlMapper.queryAgentIdAndAdvertiserId");
+		for(Advertiser a:list){
+			AdvertiserLoader.ADVERTISER_LIST.add(a);
+		}
+	}
+
+	
+
+}

+ 54 - 0
src/main/java/com/cloudcross/ssp/loader/AdvertiserLoader.java

@@ -0,0 +1,54 @@
+package com.cloudcross.ssp.loader;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.cloudcross.ssp.model.Advertiser;
+
+/**
+ * 将广告主代理商信息存入内存
+ *创建人:周俊
+ *创建时间:2016.01.04
+ *
+ */
+public class AdvertiserLoader {
+	
+	public static List<Advertiser> ADVERTISER_LIST = new ArrayList<Advertiser>();//存放所有的广告主信息
+	private static List<Advertiser> ADVERTISERID_List = new ArrayList<Advertiser>();//存放代理商相同的广告主信息
+	/**
+	 * 根据代理商ID得到该代理商的所有广告主ID字符串
+	 * @param agentId
+	 * @return {id1,id2,id3,...}
+	 */
+	public static String getAdvertiserIdByAgentId(Long agentId){
+		if(ADVERTISER_LIST != null && ADVERTISER_LIST.size() > 0){
+			for(Advertiser a:ADVERTISER_LIST){
+				//注意引用类型的比较
+				if(agentId.equals(a.getAgentId())){
+					ADVERTISERID_List.add(a);
+				}
+			}
+		}
+		return AdvertiserLoader.getAdvertiserStr();
+	}
+	
+	/**
+	 * 将同个代理商的广告主id拼接成字符串
+	 * @return {id1,id2,id3,...}
+	 */
+	private static String getAdvertiserStr(){
+		String ids = "";
+		if(ADVERTISERID_List != null && ADVERTISERID_List.size() > 0){
+			Integer count = ADVERTISERID_List.size();
+			for(int i=0;i<count;i++){
+				if(i != count-1){
+					ids += ADVERTISERID_List.get(i).getId() + ",";
+				}else{
+					ids += ADVERTISERID_List.get(i).getId();
+				}
+				
+			}
+		}
+		return ids;
+	}
+}

+ 5 - 0
src/main/java/com/cloudcross/ssp/model/mapper/advertiser.sql.xml

@@ -521,4 +521,9 @@
 		update t_advertiser set balance = #{advertiserBalance}
 		where id = #{advertiserId}
 	</update>
+	
+	<!-- 查询代理商和广告主存到内存中 -->
+	<select id="queryAgentIdAndAdvertiserId" resultType="com.cloudcross.ssp.model.Advertiser">
+		select agent_id agentId,id from t_advertiser where status != -1
+	</select>
 </mapper>

+ 5 - 0
src/main/webapp/WEB-INF/web.xml

@@ -811,6 +811,11 @@ standard deployment descriptor.
             org.springframework.web.context.ContextLoaderListener
         </listener-class>
   </listener>
+  
+  <listener> 
+       <listener-class>com.cloudcross.ssp.listener.ApplicationListener</listener-class> 
+   </listener> 
+    
   <servlet>
     <servlet-name>dispatch</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>