list = JobScheduler.getNextValidTimes("0 0/1 * * * ? *", -1, 1);
15 | for (Date item : list) {
16 | System.out.println(tools.Convert.toString(item));
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/temp/test/tools/quartz/HelloJob.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2005 - 2009 Terracotta, Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 | * use this file except in compliance with the License. You may obtain a copy
6 | * of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | *
16 | */
17 |
18 | package tools.test.quartz;
19 |
20 | import org.quartz.Job;
21 | import org.quartz.JobExecutionContext;
22 | import org.quartz.JobExecutionException;
23 |
24 | /**
25 | *
26 | * This is just a simple job that says "Hello" to the world.
27 | *
28 | *
29 | * @author Bill Kratzer
30 | */
31 | public class HelloJob implements Job {
32 |
33 | /**
34 | *
35 | * Empty constructor for job initilization
36 | *
37 | *
38 | * Quartz requires a public empty constructor so that the scheduler can instantiate the class whenever it needs.
39 | *
40 | */
41 | public HelloJob() {
42 | }
43 |
44 | /**
45 | *
46 | * Called by the {@link org.quartz.Scheduler}
when a {@link org.quartz.Trigger}
fires that
47 | * is associated with the Job
.
48 | *
49 | *
50 | * @throws JobExecutionException if there is an exception while executing the job.
51 | */
52 | public void execute(JobExecutionContext context) throws JobExecutionException {
53 |
54 | // Say Hello to the World and display the date/time
55 | System.out.println("Hello World! - " + context.getJobDetail().getJobDataMap().get("id"));
56 |
57 | // 计数
58 | SimpleExample.atomicInteger.incrementAndGet();
59 |
60 | // 睡睡
61 | try {
62 | Thread.sleep(200);
63 | } catch (InterruptedException e) {
64 | e.printStackTrace();
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/temp/test/tools/quartz/SimpleExample.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2005 - 2009 Terracotta, Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 | * use this file except in compliance with the License. You may obtain a copy
6 | * of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | *
16 | */
17 |
18 | package tools.test.quartz;
19 |
20 | import java.util.Date;
21 | import java.util.HashMap;
22 | import java.util.concurrent.atomic.AtomicInteger;
23 |
24 | import org.quartz.JobDataMap;
25 | import org.quartz.Scheduler;
26 | import org.quartz.SchedulerException;
27 | import org.quartz.SchedulerFactory;
28 | import org.quartz.impl.StdSchedulerFactory;
29 | import tools.quartz.JobScheduler;
30 |
31 | /**
32 | * This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule a job to run in
33 | * Quartz.
34 | *
35 | * @author Bill Kratzer
36 | */
37 | public class SimpleExample {
38 |
39 | public static AtomicInteger atomicInteger = new AtomicInteger(0);
40 |
41 | public void runV2() throws SchedulerException, InterruptedException {
42 |
43 | // 创建工厂
44 | SchedulerFactory sf = new StdSchedulerFactory();
45 |
46 | Scheduler sched = sf.getScheduler();
47 |
48 | // 创建JobScheduler,设置调度器
49 | JobScheduler jobScheduler = new JobScheduler(sched);
50 |
51 | // 创建一批job
52 | for (int i = 0; i < 1000; i++) {
53 | HashMap data = new HashMap();
54 | data.put("id", i);
55 | jobScheduler.addJob(new Date(), HelloJob.class, data, null);
56 | }
57 |
58 | String jobName = "cc";
59 |
60 | // 创建周期性job
61 | HashMap data = new HashMap();
62 | data.put("id", -1);
63 | jobScheduler.addJob(new Date(), HelloJob.class, data, jobName, 1, -1);
64 |
65 | // 任务重复了,添加失败
66 | boolean f = jobScheduler.addJob(new Date(), HelloJob.class, null, jobName, 1, -1);
67 | System.out.println(f + "");
68 |
69 | // 开始做
70 | jobScheduler.start();
71 |
72 | // 得到任务数据并修改
73 | Thread.sleep(200);
74 | JobDataMap jobData = jobScheduler.getJobData(jobName);
75 | if (jobData != null) {
76 | System.out.println(jobData.get("id").toString());
77 | jobData.put("id", -2);// 修改是无效的
78 | jobScheduler.setJobData(jobName, jobData);
79 | }
80 |
81 | // jobScheduler.getScheduler().pauseAll();
82 |
83 | // 删除job
84 | Thread.sleep(600);
85 | jobScheduler.deleteJob(jobName);
86 |
87 | // sleep
88 | Thread.sleep(10L * 1000L);
89 |
90 | // 看看目前还有多少任务还在做 2012-02-17
91 | System.out.println(jobScheduler.getScheduler().getCurrentlyExecutingJobs().size());
92 |
93 | // 关闭
94 | jobScheduler.shutdown(true);
95 |
96 | // 看看执行了多少job
97 | System.out.println(atomicInteger.get() + "");
98 | }
99 |
100 | public static void main(String[] args) throws Exception {
101 | SimpleExample example = new SimpleExample();
102 | example.runV2();
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/src/temp/test/tools/redis/MapTest.java:
--------------------------------------------------------------------------------
1 | package tools.test.redis;
2 |
3 | import java.net.URISyntaxException;
4 | import java.util.ArrayList;
5 | import java.util.HashMap;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | import org.junit.Assert;
10 |
11 | import org.junit.Test;
12 |
13 | import redis.clients.jedis.JedisPool;
14 | import tools.redis.RedisMap;
15 | import tools.redis.RedisUtil;
16 |
17 | public class MapTest {
18 |
19 | private static JedisPool pool;
20 |
21 | static {
22 | try {
23 | pool = createPool();
24 | } catch (URISyntaxException e) {
25 | e.printStackTrace();
26 | }
27 | }
28 |
29 | static JedisPool createPool() throws URISyntaxException {
30 | return RedisUtil
31 | .createRedisPool("redis://10.13.42.36:6379/?max_active=10&max_idle=5&max_wait=1000&timeout=1000&testKey=testValue");
32 | }
33 |
34 | @Test
35 | public void putAllTest() {
36 | RedisMap m = new RedisMap("m", pool);
37 | m.clear();
38 |
39 | Map to = new HashMap();
40 | to.put("a", "1");
41 |
42 | m.putAll(to);
43 |
44 | Assert.assertEquals(m.size(), 1);
45 | }
46 |
47 | @Test
48 | public void toArrayTest() {
49 | List list = new ArrayList();
50 | list.add("1");
51 | list.add("2");
52 |
53 | String[] arr = (String[]) list.toArray(new String[0]);
54 | Assert.assertEquals(list.size(), arr.length);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/temp/test/tools/redis/QueueTest.java:
--------------------------------------------------------------------------------
1 | package tools.test.redis;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import redis.clients.jedis.JedisPool;
7 | import redis.clients.jedis.JedisPoolConfig;
8 | import tools.redis.Executable;
9 | import tools.redis.RedisQueue;
10 |
11 | /**
12 | * 依赖commons.pool
13 | *
14 | * @author liusan.dyf
15 | */
16 | public class QueueTest {
17 | private static JedisPool pool;
18 |
19 | static {
20 | pool = createPool();
21 | }
22 |
23 | static JedisPool createPool() {
24 | JedisPoolConfig config = new JedisPoolConfig();
25 | config.setMaxActive(1);
26 | config.setMaxIdle(20);
27 | config.setMaxWait(1000);
28 | config.setTestOnBorrow(true);
29 |
30 | return new JedisPool(config, "127.0.0.1");
31 | }
32 |
33 | /**
34 | * @param args
35 | * @throws InterruptedException
36 | */
37 | public static void main(String[] args) throws InterruptedException {
38 | // // 连接断了还可以继续放池里用
39 | // Jedis client = pool.getResource();
40 | // client.disconnect();
41 | // pool.returnResource(client);
42 |
43 | // 队列
44 | final RedisQueue q = new RedisQueue(pool, "q");
45 | // q.add("1");
46 | // q.add("2");
47 | // q.remove("2");
48 | List list = new ArrayList();
49 | list.add("a");
50 | list.add("b");
51 |
52 | q.addAll(list);
53 | System.out.println("size is " + q.size());
54 | System.out.println("first is " + q.element());
55 |
56 | // 开个线程,来停止拉取
57 | Thread th = new Thread(new Runnable() {
58 | @Override
59 | public void run() {
60 | try {
61 | Thread.sleep(1000 * 10);
62 | } catch (InterruptedException e) {
63 | e.printStackTrace();
64 | }
65 |
66 | q.stopBlockingPoll();
67 | }
68 | });
69 | th.setDaemon(true);
70 | // th.start();
71 |
72 | // // 放入队列速度测试
73 | // long start = System.currentTimeMillis();
74 | // for (int i = 0; i < 100000; i++) {
75 | // q.add("http://...");
76 | // }
77 | // System.out.println(System.currentTimeMillis() - start);
78 | //
79 | // return;
80 |
81 | // 拉取
82 | q.setCallback(new Executable() {
83 | int i = 0;
84 |
85 | @Override
86 | public void execute(Object value) {
87 | System.out.println(value);
88 | i++;
89 | if ((i % 1000) == 0)
90 | System.out.println(i);
91 | }
92 | });
93 |
94 | q.setErrorCallback(new Executable() {
95 | @Override
96 | public void execute(Object value) {
97 | // 转换参数
98 | Object[] arr = (Object[]) value;
99 |
100 | // 第一个参数
101 | RedisQueue r = (RedisQueue) arr[0];
102 | r.getPool().destroy();
103 |
104 | // 第二个参数是异常信息
105 | // System.out.println(arr[1].getClass().toString());
106 |
107 | // 服务器突然中断了,down了 2012-02-07
108 | System.out.println("redis server is down,because of " + arr[1].getClass().getName());
109 |
110 | // 等会儿继续
111 | try {
112 | Thread.sleep(1000 * 5);
113 | } catch (InterruptedException e) {
114 |
115 | }
116 |
117 | // 继续连接
118 | r.setPool(createPool());
119 | q.startBlockingPoll();
120 | }
121 | });
122 |
123 | q.startBlockingPoll();
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/src/test/java/tools/test/BeanTest.java:
--------------------------------------------------------------------------------
1 | package tools.test;
2 |
3 | import java.util.Date;
4 | import java.util.Map;
5 |
6 | public class BeanTest {
7 | public static void main(String[] args) {
8 | A a = new A();
9 | a.setId(10086);
10 |
11 | B b = new B();
12 |
13 | // copy
14 | tools.BeanUtil.copy(a, b);
15 |
16 | System.out.println(tools.Json.toString(b));
17 |
18 | // fill
19 | Map map = tools.MapUtil.create();
20 | map.put("name", "六三");
21 | map.put("date", new Date());
22 | tools.BeanUtil.fill(b, map);
23 |
24 | System.out.println(tools.Json.toString(b));
25 | }
26 | }
27 |
28 | class A {
29 | private long id;
30 |
31 | public long getId() {
32 | return id;
33 | }
34 |
35 | public void setId(long id) {
36 | this.id = id;
37 | }
38 | }
39 |
40 | class B {
41 | private long id;
42 | private String name;
43 | private Date date;
44 |
45 | public long getId() {
46 | return id;
47 | }
48 |
49 | public void setId(long id) {
50 | this.id = id;
51 | }
52 |
53 | public String getName() {
54 | return name;
55 | }
56 |
57 | public void setName(String name) {
58 | this.name = name;
59 | }
60 |
61 | public Date getDate() {
62 | return date;
63 | }
64 |
65 | public void setDate(Date date) {
66 | this.date = date;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/test/java/tools/test/BitSetTest.java:
--------------------------------------------------------------------------------
1 | package tools.test;
2 |
3 | import java.util.BitSet;
4 |
5 | public class BitSetTest {
6 | /**
7 | * @param args
8 | */
9 | public static void main(String[] args) {
10 | BitSet bm1 = new BitSet(7);
11 | System.out.println(bm1.isEmpty() + "--" + bm1.size()); // 64
12 |
13 | BitSet bm2 = new BitSet(63);
14 | System.out.println(bm2.isEmpty() + "--" + bm2.size());// 64
15 |
16 | BitSet bm3 = new BitSet(65);
17 | System.out.println(bm3.isEmpty() + "--" + bm3.size());// 128
18 |
19 | BitSet bm4 = new BitSet(111);
20 | System.out.println(bm4.isEmpty() + "--" + bm4.size());// 128
21 |
22 | System.out.println(bm4.get(1024)); // false
23 | bm4.set(1024, true); // ���Զ����ݣ����Dz����̰߳�ȫ��
24 | System.out.println(bm4.isEmpty() + "--" + bm4.size());// �Ѿ����ݣ�1088
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/test/java/tools/test/CategoryTest.java:
--------------------------------------------------------------------------------
1 | package tools.test;
2 |
3 | import java.util.Map;
4 |
5 | import org.junit.Assert;
6 | import org.junit.Test;
7 |
8 | import tools.CategoryService;
9 | import tools.MapUtil;
10 |
11 | /**
12 | * @author 六三
13 | * @version 1.0
14 | * @since 2014年12月12日
15 | */
16 | public class CategoryTest {
17 | private CategoryService entry = getService();
18 |
19 | private CategoryService getService() {
20 | // 构造树
21 | Map map = MapUtil.create();
22 | map.put(1, 0);
23 | map.put(2, 1);
24 | map.put(3, 1);
25 | map.put(4, 3);
26 | map.put(5, 3);
27 | map.put(7, 0);
28 | map.put(8, 7);
29 | map.put(9, 8);
30 |
31 | // 错误的干扰数据 2016-7-22 09:45:13 by liusan.dyf
32 | // map.put(-1, -1);
33 | // map.put(0, -1);
34 | map.put(6, 6);
35 |
36 | map.put(1, 5);// 组成环状数据
37 |
38 | // 匹配测试
39 | CategoryService entry = new CategoryService();
40 | entry.init(map);
41 |
42 | return entry;
43 | }
44 |
45 | @Test
46 | public void isChildOfTest() {
47 | Assert.assertEquals(true, entry.isChildOf(5, 1));
48 | Assert.assertEquals(false, entry.isChildOfAny(5, "4,2"));
49 | Assert.assertEquals(true, entry.isChildOfAny(5, "3"));
50 | Assert.assertEquals(false, entry.isChildOf(5, 20));
51 |
52 | Assert.assertEquals(false, entry.isChildOf(6, 1));// false,节点指向了自己
53 | Assert.assertEquals(false, entry.isChildOfAny(5, "4,2"));// false,环状数据,已达最大查找次数
54 | Assert.assertEquals(true, entry.isChildOfAny(9, "7,2"));// true
55 | Assert.assertEquals(false, entry.isChildOfAny(0, "7,2"));// false
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/test/java/tools/test/DateTest.java:
--------------------------------------------------------------------------------
1 | package tools.test;
2 |
3 | import java.util.Date;
4 |
5 | import org.junit.Assert;
6 | import org.junit.Test;
7 |
8 | public class DateTest {
9 | final static String DATE_FORMAT = "yyyy-MM-dd";
10 |
11 | @Test
12 | public void specialTest() {
13 | // getSpecialDate
14 | // System.out.println(tools.DateTime.getSpecialDate(null, -1, -1, 0, 0));// 整点
15 | // System.out.println(tools.DateTime.getSpecialDate(null, -1, 0, 0, 0));// 0点
16 |
17 | System.getProperties();
18 | }
19 |
20 | @Test
21 | public void dayOfWeekTest() {
22 | Assert.assertEquals("2014-01-13",
23 | tools.DateTime.format(tools.DateTime.getMonday("2014-01-13", DATE_FORMAT), DATE_FORMAT));
24 | Assert.assertEquals("2014-01-06",
25 | tools.DateTime.format(tools.DateTime.getMonday("2014-01-07", DATE_FORMAT), DATE_FORMAT));
26 | }
27 |
28 | @Test
29 | public void parseTest() {
30 | final String s = "2013-01-09";
31 |
32 | Date d = tools.DateTime.parse(s, DATE_FORMAT);
33 | System.out.println(d);
34 |
35 | new tools.code.RunTimer().run("alignTest", 100000, new Runnable() {
36 | @Override
37 | public void run() {
38 | // tools.Convert.toDateTime(s, "yyyy-MM-dd");// 255
39 | // tools.DateTime.parse(s, "yyyy-MM-dd");// 205
40 | tools.MySqlFunction.toDate(s);// 842 - 278
41 | }
42 | });
43 | }
44 |
45 | @Test
46 | public void formatTest() {
47 | String s = "2013-01-09";
48 | Date d = tools.DateTime.parse(s, DATE_FORMAT);
49 | Assert.assertEquals(s, tools.DateTime.format(d, DATE_FORMAT));
50 | Assert.assertEquals(s, tools.DateTime.format(d, 0, DATE_FORMAT));
51 | Assert.assertEquals("2013-01-08", tools.DateTime.format(d, -1, DATE_FORMAT));
52 | Assert.assertEquals("2013-01-10", tools.DateTime.format(d, 1, DATE_FORMAT));
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/test/java/tools/test/ExecutorServiceTest.java:
--------------------------------------------------------------------------------
1 | package tools.test;
2 |
3 | import java.util.concurrent.ExecutorService;
4 | import java.util.concurrent.Executors;
5 | import java.util.concurrent.Future;
6 |
7 | /**
8 | * // from http://my.oschina.net/bairrfhoinn/blog/177639
9 | *
10 | * @author omeweb
11 | * @version 1.0
12 | * @since 2014年12月26日
13 | */
14 | public class ExecutorServiceTest {
15 | public static void main(String[] args) throws Throwable {
16 | Future> f = async();
17 | System.out.println("begin");
18 | f.get();// 这里只是确保执行完毕
19 | System.out.println("done");
20 | }
21 |
22 | public static Future> async() {
23 | ExecutorService executorService = Executors.newSingleThreadExecutor();
24 |
25 | Future> f = executorService.submit(new Runnable() {
26 | public void run() {
27 | tools.Global.sleep(10000);
28 | }
29 | });
30 |
31 | executorService.shutdown();
32 |
33 | return f;
34 |
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/test/java/tools/test/FalseSharing.java:
--------------------------------------------------------------------------------
1 | package tools.test;
2 |
3 | /**
4 | * from http://ifeve.com/false-sharing/
5 | */
6 | public final class FalseSharing implements Runnable {
7 | public final static int NUM_THREADS = 4; // change
8 | public final static long ITERATIONS = 500L * 1000L * 1000L;
9 | private final int arrayIndex;
10 |
11 | private static VolatileLong[] longs = new VolatileLong[NUM_THREADS];
12 |
13 | static {
14 | for (int i = 0; i < longs.length; i++) {
15 | longs[i] = new VolatileLong();
16 | }
17 | }
18 |
19 | public FalseSharing(final int arrayIndex) {
20 | this.arrayIndex = arrayIndex;
21 | }
22 |
23 | public static void main(final String[] args) throws Exception {
24 | final long start = System.nanoTime();
25 | runTest();
26 | System.out.println("duration = " + (System.nanoTime() - start));
27 | }
28 |
29 | private static void runTest() throws InterruptedException {
30 | Thread[] threads = new Thread[NUM_THREADS];
31 |
32 | for (int i = 0; i < threads.length; i++) {
33 | threads[i] = new Thread(new FalseSharing(i));
34 | }
35 |
36 | for (Thread t : threads) {
37 | t.start();
38 | }
39 |
40 | for (Thread t : threads) {
41 | t.join();
42 | }
43 | }
44 |
45 | public void run() {
46 | long i = ITERATIONS + 1;
47 | while (0 != --i) {
48 | longs[arrayIndex].value = i;
49 | }
50 | }
51 |
52 | public final static class VolatileLong {
53 | public volatile long value = 0L;
54 | // public long p1, p2, p3, p4, p5, p6; // comment out
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/test/java/tools/test/FileTest.java:
--------------------------------------------------------------------------------
1 | package tools.test;
2 |
3 | import java.io.File;
4 | import java.io.FileInputStream;
5 | import java.util.ArrayList;
6 | import java.util.List;
7 | import java.util.Map;
8 | import java.util.Map.Entry;
9 | import java.util.Set;
10 |
11 | import org.apache.commons.io.IOUtils;
12 | import org.apache.commons.io.LineIterator;
13 | import org.junit.Assert;
14 | import org.junit.Test;
15 |
16 | public class FileTest {
17 | @Test
18 | public void specialTest() {
19 | File f = new File("d:/");
20 | Assert.assertEquals(false, f.isFile());
21 | Assert.assertEquals(true, new File("d:/h.txt").isFile());
22 |
23 | // 如果File不存在,则isFile也会返回false 2014-06-11 by liusan.dyf
24 |
25 | Object obj = null;
26 | List