Signed-off-by: lixiangrong <lixiangrong@pxene.com>
This commit is contained in:
lixiangrong 2016-03-23 13:59:20 +08:00
parent 5f10ddb3db
commit efdecfb038
7 changed files with 173 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -268,19 +269,24 @@ public class JedisUtil {
* @param unit * @param unit
* @return * @return
*/ */
@SuppressWarnings("resource")
private static boolean trylock(String key) { private static boolean trylock(String key) {
Jedis jedis = null; Jedis jedis = null;
TimeUnit timeUnit = TimeUnit.SECONDS;
long timeout = 4;
try { try {
jedis = getJedis(); jedis = getJedis();
long nano = System.nanoTime();
do { do {
Long i = jedis.setnx(key + CONST_STR, key); Long i = jedis.setnx(key + CONST_STR, key);
if (i == 1) { if (i == 1) {
jedis.expire(key + CONST_STR, DEFAULT_SINGLE_EXPIRE_TIME); jedis.expire(key + CONST_STR, DEFAULT_SINGLE_EXPIRE_TIME);
return Boolean.TRUE; return Boolean.TRUE;
} else { // 存在锁
String desc = jedis.get(key + CONST_STR);
System.out.println(desc);
} }
Thread.sleep(100); Thread.sleep(300);
} while (true); } while ((System.nanoTime() - nano) < timeUnit.toNanos(timeout));
} catch (RuntimeException e) { } catch (RuntimeException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -3,6 +3,6 @@ redis.pool.maxIdle=200
redis.pool.maxWait=1000 redis.pool.maxWait=1000
redis.pool.testOnBorrow=true redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true redis.pool.testOnReturn=true
redis.ip=centos-001 redis.ip=192.168.3.166
redis.port=7379 redis.port=7379
redis.password= redis.password=

View File

@ -0,0 +1,23 @@
package osc.git.eh3.test;
import java.math.BigDecimal;
public interface IChargeCounter {
/**
* 按点击计费
*
* @param campaignid
* @param groupid
* @param cost
*/
public void chargeForThisResult(String campaignid, String groupid, BigDecimal cost);
/**
* 投放次数控制
*
* @param groupid
* @param count
* @param type
*/
public void counterControlForThisSumResult(String groupid, int count, String type);
}

View File

@ -1,7 +1,5 @@
package osc.git.eh3.test; package osc.git.eh3.test;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
@ -9,11 +7,8 @@ import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import net.sf.json.JSONObject;
import osc.git.eh3.utils.AESEncrypter; import osc.git.eh3.utils.AESEncrypter;
import osc.git.eh3.utils.Base64; import osc.git.eh3.utils.Base64;
import osc.git.eh3.utils.GeoHash;
import osc.git.eh3.utils.HttpClientUtil;
public class TestCode { public class TestCode {
@ -178,9 +173,10 @@ public class TestCode {
// System.out.println(AESEncrypter.encrypt("lixiangrong")); // System.out.println(AESEncrypter.encrypt("lixiangrong"));
// System.out.println(AESEncrypter.decrypt(AESEncrypter.encrypt("lixiangrong"))); // System.out.println(AESEncrypter.decrypt(AESEncrypter.encrypt("lixiangrong")));
//
// System.out.println(AESEncrypter.encrypt("lixiangrong","ca048b18cac58865a8")); // System.out.println(AESEncrypter.encrypt("fa4d7d90618dcba5fa1d969cffc04def","002020202"));
// System.out.println(AESEncrypter.decrypt(AESEncrypter.encrypt("lixiangrong","ca048b18cac58865a8"),"ca048b18cac58865a8")); // System.out.println(AESEncrypter.decrypt(AESEncrypter.encrypt("lixiangrong","0"),"0"));
// System.out.println(Base64.encodeToString(AESEncrypter.encrypt("fa4d7d90618dcba5fa1d969cffc04def","002020202").getBytes(), false));
// byte[] bytes = "lixiangrong".getBytes(); // byte[] bytes = "lixiangrong".getBytes();
// for (int i = 0; i < bytes.length; i++) { // for (int i = 0; i < bytes.length; i++) {
@ -189,14 +185,26 @@ public class TestCode {
// System.out.println(Base64.encodeToString("lixiangrong".getBytes(), false)); // System.out.println(Base64.encodeToString("lixiangrong".getBytes(), false));
double lon1 = 109.0145193759; // double lon1 = 109.0145193759;
double lat1 = 34.236080797698; // double lat1 = 34.236080797698;
System.out.println(GeoHash.encode(lat1, lon1)); // System.out.println(GeoHash.encode(lat1, lon1));
System.out.println(GeoHash.decode("wmtdgn5esrb1")[0]+" "+GeoHash.decode("wmtdgn5esrb1")[1]); // System.out.println(GeoHash.decode("wmtdgn5esrb1")[0]+" "+GeoHash.decode("wmtdgn5esrb1")[1]);
// String url = "http://api.map.baidu.com/place/v2/search?query=银行&location=39.915,116.404&radius=2000&output=json&ak=LCG4dyrXyadeD8hFhi8SGCv6"; // String url = "http://api.map.baidu.com/place/v2/search?query=银行&location=39.915,116.404&radius=2000&output=json&ak=LCG4dyrXyadeD8hFhi8SGCv6";
// System.out.println(HttpClientUtil.sendGet(url)); // System.out.println(HttpClientUtil.sendGet(url));
// JSONArray array = new JSONArray();
// array.add("1");
// array.add("2");
// array.add("3");
// array.add("4");
// array.add("5");
// List<String> list = JSONArray.toList(array, new String(), new JsonConfig());
// System.out.println(list);
System.out.println(System.nanoTime());
System.out.println(System.nanoTime());
} }
public static Long parseDate(String s) { public static Long parseDate(String s) {

View File

@ -0,0 +1,11 @@
package osc.git.eh3.test;
public class TestCounter {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 8000; i++) {
new Thread(new TestThread()).start();
// Thread.sleep(5);
}
}
}

View File

@ -0,0 +1,51 @@
package osc.git.eh3.test;
import java.util.HashMap;
import java.util.Map;
import com.caucho.hessian.client.HessianProxyFactory;
import osc.git.eh3.redis.JedisUtil;
import osc.git.eh3.utils.HttpClientUtil;
public class TestThread implements Runnable {
@Override
public void run() {
HessianProxyFactory factory = new HessianProxyFactory();
IChargeCounter readLogs;
for(int i=0;i<5;i++){
try {
readLogs = (IChargeCounter) factory.create(IChargeCounter.class, "http://192.168.1.135:8080/dsp-counter/remote/chargeCounter");
readLogs.counterControlForThisSumResult("100003", 1, "m");
//System.out.println(JedisUtil.getStr("dsp_counter_100003"));
break;
} catch (Exception e) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
// Map<String, String> postParam = new HashMap<String, String>();
// postParam.put("groupid", "100003");
// postParam.put("count", "1");
// postParam.put("type", "m");
// for(int i=0;i<5;i++){
// try {
// HttpClientUtil.sendPostParam("http://192.168.1.135:8080/dsp-counter/remote/chargeCounter/counterControlForThisSumResult", postParam);
//// HttpClientUtil.sendPost("http://192.168.1.135:8080/dsp-counter/remote/chargeCounter/counterControlForThisSumResult", "groupid=100003&count=1&type=m");
// break;
// } catch (Exception e) {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
// }
// }
}
}

View File

@ -1,8 +1,10 @@
package osc.git.eh3.utils; package osc.git.eh3.utils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -313,4 +315,60 @@ public class HttpClientUtil {
} }
return result; return result;
} }
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数请求参数应该是 name1=value1&name2=value2 的形式
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
} }