diff --git a/src/main/java/osc/git/eh3/redis/JedisUtil.java b/src/main/java/osc/git/eh3/redis/JedisUtil.java index 0010da4..27c08e7 100644 --- a/src/main/java/osc/git/eh3/redis/JedisUtil.java +++ b/src/main/java/osc/git/eh3/redis/JedisUtil.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.TimeUnit; import org.springframework.util.StringUtils; @@ -268,19 +269,24 @@ public class JedisUtil { * @param unit * @return */ - @SuppressWarnings("resource") private static boolean trylock(String key) { Jedis jedis = null; + TimeUnit timeUnit = TimeUnit.SECONDS; + long timeout = 4; try { jedis = getJedis(); + long nano = System.nanoTime(); do { Long i = jedis.setnx(key + CONST_STR, key); if (i == 1) { jedis.expire(key + CONST_STR, DEFAULT_SINGLE_EXPIRE_TIME); return Boolean.TRUE; + } else { // 存在锁 + String desc = jedis.get(key + CONST_STR); + System.out.println(desc); } - Thread.sleep(100); - } while (true); + Thread.sleep(300); + } while ((System.nanoTime() - nano) < timeUnit.toNanos(timeout)); } catch (RuntimeException e) { e.printStackTrace(); } catch (InterruptedException e) { diff --git a/src/main/java/osc/git/eh3/redis/redis.properties b/src/main/java/osc/git/eh3/redis/redis.properties index f776b91..0b490ac 100644 --- a/src/main/java/osc/git/eh3/redis/redis.properties +++ b/src/main/java/osc/git/eh3/redis/redis.properties @@ -3,6 +3,6 @@ redis.pool.maxIdle=200 redis.pool.maxWait=1000 redis.pool.testOnBorrow=true redis.pool.testOnReturn=true -redis.ip=centos-001 +redis.ip=192.168.3.166 redis.port=7379 redis.password= \ No newline at end of file diff --git a/src/main/java/osc/git/eh3/test/IChargeCounter.java b/src/main/java/osc/git/eh3/test/IChargeCounter.java new file mode 100644 index 0000000..5a0b4fb --- /dev/null +++ b/src/main/java/osc/git/eh3/test/IChargeCounter.java @@ -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); +} \ No newline at end of file diff --git a/src/main/java/osc/git/eh3/test/TestCode.java b/src/main/java/osc/git/eh3/test/TestCode.java index 24a94e1..f0f7a9a 100644 --- a/src/main/java/osc/git/eh3/test/TestCode.java +++ b/src/main/java/osc/git/eh3/test/TestCode.java @@ -1,7 +1,5 @@ package osc.git.eh3.test; -import java.net.URLDecoder; -import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; @@ -9,11 +7,8 @@ import java.util.Date; import java.util.HashSet; import java.util.Set; -import net.sf.json.JSONObject; import osc.git.eh3.utils.AESEncrypter; import osc.git.eh3.utils.Base64; -import osc.git.eh3.utils.GeoHash; -import osc.git.eh3.utils.HttpClientUtil; public class TestCode { @@ -178,9 +173,10 @@ public class TestCode { // System.out.println(AESEncrypter.encrypt("lixiangrong")); // System.out.println(AESEncrypter.decrypt(AESEncrypter.encrypt("lixiangrong"))); -// -// System.out.println(AESEncrypter.encrypt("lixiangrong","ca048b18cac58865a8")); -// System.out.println(AESEncrypter.decrypt(AESEncrypter.encrypt("lixiangrong","ca048b18cac58865a8"),"ca048b18cac58865a8")); + +// System.out.println(AESEncrypter.encrypt("fa4d7d90618dcba5fa1d969cffc04def","002020202")); +// 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(); // for (int i = 0; i < bytes.length; i++) { @@ -189,14 +185,26 @@ public class TestCode { // System.out.println(Base64.encodeToString("lixiangrong".getBytes(), false)); - double lon1 = 109.0145193759; - double lat1 = 34.236080797698; - System.out.println(GeoHash.encode(lat1, lon1)); - System.out.println(GeoHash.decode("wmtdgn5esrb1")[0]+" "+GeoHash.decode("wmtdgn5esrb1")[1]); +// double lon1 = 109.0145193759; +// double lat1 = 34.236080797698; +// System.out.println(GeoHash.encode(lat1, lon1)); +// 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"; // 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 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) { diff --git a/src/main/java/osc/git/eh3/test/TestCounter.java b/src/main/java/osc/git/eh3/test/TestCounter.java new file mode 100644 index 0000000..dddbf12 --- /dev/null +++ b/src/main/java/osc/git/eh3/test/TestCounter.java @@ -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); + } + } +} \ No newline at end of file diff --git a/src/main/java/osc/git/eh3/test/TestThread.java b/src/main/java/osc/git/eh3/test/TestThread.java new file mode 100644 index 0000000..42079f2 --- /dev/null +++ b/src/main/java/osc/git/eh3/test/TestThread.java @@ -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 postParam = new HashMap(); +// 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(); +// } +// } +// } + } +} diff --git a/src/main/java/osc/git/eh3/utils/HttpClientUtil.java b/src/main/java/osc/git/eh3/utils/HttpClientUtil.java index 6467d07..6079607 100644 --- a/src/main/java/osc/git/eh3/utils/HttpClientUtil.java +++ b/src/main/java/osc/git/eh3/utils/HttpClientUtil.java @@ -1,8 +1,10 @@ package osc.git.eh3.utils; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; @@ -313,4 +315,60 @@ public class HttpClientUtil { } 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; + } }