BeanstalkClient学习

时间:2024-01-04 16:10:02

针对BeanstalkClient-1.4.6.jar

  • 生产者

示例代码:

package com.lky.test;

import java.io.UnsupportedEncodingException;

import org.junit.Ignore;
import org.junit.Test; import com.surftools.BeanstalkClientImpl.ClientImpl; /**
* @Title: produceTest.java
* @Package com.lky.test
* @Description: beanstalkClient produce使用学习
* @author lky
* @date 2015年10月20日 上午10:12:49
* @version V1.0
*/ public class produceTest { @Test
@Ignore
public void produce1() throws UnsupportedEncodingException{
ClientImpl client=new ClientImpl("10.21.25.196",11300,true);
client.useTube("phone"); client.put(3, 0 ,20, new String("中兴").getBytes("utf-8"));
client.put(1, 0, 20, new String("小米").getBytes("utf-8"));
client.put(2, 0, 20, new String("苹果").getBytes("utf-8"));
client.close();
} @Test
public void produce2() throws UnsupportedEncodingException{
ClientImpl client=new ClientImpl("10.21.25.196",11300,true);
client.useTube("log"); client.put(2, 0, 20, new String("123456").getBytes("utf-8"));
client.put(5, 0, 20, new String("4541212").getBytes("utf-8"));
client.put(3, 0, 20, new String("1212121212").getBytes("utf-8"));
client.close();
}
}
  • 消费者

示例代码:

package com.lky.test;

import java.io.UnsupportedEncodingException;
import java.util.Random;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import com.surftools.BeanstalkClient.Job;
import com.surftools.BeanstalkClientImpl.ClientImpl; /**
* @Title: consumerTest.java
* @Package com.lky.test
* @Description: beanstatlked 消费者学习 (job交互,tube交互)
* @author lky
* @date 2015年10月20日 上午10:15:19
* @version V1.0
*/
public class consumerTest { @Before
@Ignore
public void loadUp(){
ClientImpl client = new ClientImpl("10.21.25.196", 11300, true);
client.useTube("log");
client.kick(3);//将处于bury状态下的job重新放入ready队列中
client.close();
} @Test
public void consumer1() throws UnsupportedEncodingException {
ClientImpl client = new ClientImpl("10.21.25.196", 11300, true);
client.watch("log");
client.ignore("default"); Job job=null;
while((job=client.reserve(20))!=null){
System.out.println(job.getJobId()+"---------->"+new String(job.getData(),"utf-8"));
// client.bury(job.getJobId(), new Random(10).nextInt(11));
// client.delete(job.getJobId());
client.release(job.getJobId(), new Random(100).nextInt(11), new Random(100).nextInt(11));
// client.touch(job.getJobId());
}
client.close();
}
}
  • 状态监测

示例代码:

package com.lky.test;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import com.surftools.BeanstalkClientImpl.ClientImpl; /**
* @Title: stateTest.java
* @Package com.lky.test
* @Description: beanstalked队列中 处于各种状态下job的统计
* @author lky
* @date 2015年10月20日 上午10:16:59
* @version V1.0
*/
public class stateTest { @Before
public void testBefore(){
System.out.println("-------------------分隔符------------------");
} @After
public void testAfter(){
System.out.println("-------------------分隔符------------------");
} @Test
public void testStatusLog(){
ClientImpl client=new ClientImpl("10.21.25.196",11300,true);
client.useTube("log");
Map<String, String> info=client.statsTube("log");
for(Entry<String, String>entry:info.entrySet()){
System.out.println(entry.getKey()+"---------->"+entry.getValue());
}
client.close();
} @Test
public void testStatusPhone(){
ClientImpl client=new ClientImpl("10.21.25.196",11300,true);
client.useTube("phone");
Map<String, String> info=client.statsTube("phone");
for(Entry<String, String>entry:info.entrySet()){
System.out.println(entry.getKey()+"---------->"+entry.getValue());
}
client.close();
} @Test
@Ignore
public void testStatusDefault(){
ClientImpl client=new ClientImpl("10.21.25.196",11300,true);
client.useTube("default");
Map<String, String> info=client.statsTube("default");
for(Entry<String, String>entry:info.entrySet()){
System.out.println(entry.getKey()+"---------->"+entry.getValue());
}
client.close();
} @Test
@Ignore
public void testStatus(){
ClientImpl client=new ClientImpl("10.21.25.196",11300,true);
Map<String, String> info=client.stats();
for(Entry<String, String>entry:info.entrySet()){
System.out.println(entry.getKey()+"---------->"+entry.getValue());
}
client.close();
} @Test
@Ignore
public void testStatusListTube(){
ClientImpl client=new ClientImpl("10.21.25.196",11300,true);
List<String> info=client.listTubes();
for(String key:info){
System.out.println(key);
}
client.close();
} @Test
@Ignore
public void testStatusListTubeWatch(){
ClientImpl client=new ClientImpl("10.21.25.196",11300,true);
client.watch("log");
client.watch("phone");
client.ignore("default");
List<String> info=client.listTubesWatched();
for(String key:info){
System.out.println(key);
}
client.close();
} }