少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

package com.abin.lee.queue;

import java.util.Queue;

public interface IMakeQueue {
 public void addQueue(Object obj);
 public boolean hasQueue();
 public Object getQueueHeader();
 public int queueSize();
 public boolean delQueue();
}




package com.abin.lee.queue;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class MakeQueue implements IMakeQueue{
 private static Queue queue=new LinkedBlockingQueue();

 public void addQueue(Object obj) {
  queue.offer(obj);
 }

 public boolean hasQueue() {
  boolean flag=false;
  if(queue.isEmpty()){
   flag=true;
  }
  return flag;
 }

 public Object getQueueHeader() {
  Object obj=queue.peek();
  return obj;
 }

 public int queueSize() {
  int queueSize=queue.size();
  return queueSize;
 }

 public boolean delQueue() {
  boolean flag=false;
  Object obj=queue.poll();
  if(obj!=null){
   flag=true;
  }
  return flag;
 }
 
}






package com.abin.lee.queue;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * 获取spring容器,以访问容器中定义的其他bean
 *
 * @author lyltiger
 * @since MOSTsView 3.0 2009-11-16
 */
public class SpringContextUtil implements ApplicationContextAware {

 // Spring应用上下文环境
 private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
   "com/abin/lee/spring/applicationContext-queue.xml");

 /**
  * 实现ApplicationContextAware接口的回调方法,设置上下文环境
  *
  * @param applicationContext
  */
 public void setApplicationContext(ApplicationContext applicationContext) {
  SpringContextUtil.applicationContext = applicationContext;
 }

 /**
  * @return ApplicationContext
  */
 public static ApplicationContext getApplicationContext() {
  return applicationContext;
 }

 /**
  * 获取对象 这里重写了bean方法,起主要作用
  *
  * @param name
  * @return Object 一个以所给名字注册的bean的实例
  * @throws BeansException
  */
 public static Object getBean(String name) throws BeansException {
  return applicationContext.getBean(name);
 }

}





package com.abin.lee.queue;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QueueServlet extends HttpServlet{
 
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  Map map=request.getParameterMap();
  String name1=(String)((Object[])map.get("name1"))[0];
  String name2=(String)((Object[])map.get("name2"))[0];
  MakeQueue makeQueue = (MakeQueue)SpringContextUtil.getBean("makeQueue");//bean的名称
  System.out.println(makeQueue.queueSize());
  makeQueue.addQueue(name1);
  makeQueue.addQueue(name2);
  System.out.println(makeQueue.queueSize());
  
  ServletOutputStream out=response.getOutputStream();
  BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
  writer.write("success");
  writer.flush();
  writer.close();
 }
}





package com.abin.lee.spring;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class SpringUnit extends TestCase {
 private static final String HTTP_URL="http://localhost:1010/WebService/servlet/QueueServlet";
 public void testBean() {
  HttpClient client=new DefaultHttpClient();
  HttpPost post=new HttpPost(HTTP_URL);
  try {
   List<NameValuePair> nvps=new ArrayList<NameValuePair>();
   nvps.add(new BasicNameValuePair("name1", "lee"));
   nvps.add(new BasicNameValuePair("name2", "abin"));
   post.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
   HttpResponse response=client.execute(post);
   HttpEntity entity=response.getEntity();
   String result=EntityUtils.toString(entity);
   System.out.println("result="+result);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}







<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <!-- Queue全局队列 -->
 <bean id="makeQueue" class="com.abin.lee.queue.MakeQueue" scope="singleton" />
 

</beans>

posted on 2012-08-20 12:25 abin 阅读(2675) 评论(0)  编辑  收藏 所属分类: java集合类

只有注册用户登录后才能发表评论。


网站导航: