Search in sources :

Example 26 with ByteSequence

use of org.apache.activemq.util.ByteSequence in project activemq-artemis by apache.

the class JobSchedulerTest method testAddStopThenDeliver.

@Test
public void testAddStopThenDeliver() throws Exception {
    final int COUNT = 10;
    final CountDownLatch latch = new CountDownLatch(COUNT);
    long time = 2000;
    for (int i = 0; i < COUNT; i++) {
        String test = new String("test" + i);
        scheduler.schedule("id" + i, new ByteSequence(test.getBytes()), "", time, 1000, -1);
    }
    File directory = store.getDirectory();
    tearDown();
    startStore(directory);
    scheduler.addListener(new JobListener() {

        @Override
        public void scheduledJob(String id, ByteSequence job) {
            latch.countDown();
        }
    });
    assertTrue(latch.getCount() == COUNT);
    latch.await(3000, TimeUnit.SECONDS);
    assertTrue(latch.getCount() == 0);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) ByteSequence(org.apache.activemq.util.ByteSequence) Test(org.junit.Test)

Example 27 with ByteSequence

use of org.apache.activemq.util.ByteSequence in project activemq-artemis by apache.

the class JobSchedulerTest method testgetAllJobs.

@Test
public void testgetAllJobs() throws Exception {
    final int COUNT = 10;
    final String ID = "id:";
    long time = 20000;
    for (int i = 0; i < COUNT; i++) {
        String str = new String("test" + i);
        scheduler.schedule(ID + i, new ByteSequence(str.getBytes()), "", time, 10 + i, -1);
    }
    List<Job> list = scheduler.getAllJobs();
    assertEquals(list.size(), COUNT);
    int count = 0;
    for (Job job : list) {
        assertEquals(job.getJobId(), ID + count);
        count++;
    }
}
Also used : ByteSequence(org.apache.activemq.util.ByteSequence) Test(org.junit.Test)

Example 28 with ByteSequence

use of org.apache.activemq.util.ByteSequence in project activemq-artemis by apache.

the class JobSchedulerTest method testAddCronAndByteSequence.

@Test
public void testAddCronAndByteSequence() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    scheduler.addListener(new JobListener() {

        @Override
        public void scheduledJob(String id, ByteSequence job) {
            latch.countDown();
        }
    });
    Calendar current = Calendar.getInstance();
    current.add(Calendar.MINUTE, 1);
    int minutes = current.get(Calendar.MINUTE);
    int hour = current.get(Calendar.HOUR_OF_DAY);
    int day = current.get(Calendar.DAY_OF_WEEK) - 1;
    String cronTab = String.format("%d %d * * %d", minutes, hour, day);
    String str = new String("test1");
    scheduler.schedule("id:1", new ByteSequence(str.getBytes()), cronTab, 0, 0, 0);
    // need a little slack so go over 60 seconds
    assertTrue(latch.await(70, TimeUnit.SECONDS));
    assertEquals(0, latch.getCount());
}
Also used : Calendar(java.util.Calendar) CountDownLatch(java.util.concurrent.CountDownLatch) ByteSequence(org.apache.activemq.util.ByteSequence) Test(org.junit.Test)

Example 29 with ByteSequence

use of org.apache.activemq.util.ByteSequence in project activemq-artemis by apache.

the class JobSchedulerTest method testGetExecutionCount.

@Test
public void testGetExecutionCount() throws Exception {
    final String jobId = "Job-1";
    long time = 10000;
    final CountDownLatch done = new CountDownLatch(10);
    String str = new String("test");
    scheduler.schedule(jobId, new ByteSequence(str.getBytes()), "", time, 1000, 10);
    int size = scheduler.getAllJobs().size();
    assertEquals(size, 1);
    scheduler.addListener(new JobListener() {

        @Override
        public void scheduledJob(String id, ByteSequence job) {
            LOG.info("Job executed: {}", 11 - done.getCount());
            done.countDown();
        }
    });
    List<Job> jobs = scheduler.getNextScheduleJobs();
    assertEquals(1, jobs.size());
    Job job = jobs.get(0);
    assertEquals(jobId, job.getJobId());
    assertEquals(0, job.getExecutionCount());
    assertTrue("Should have fired ten times.", done.await(60, TimeUnit.SECONDS));
    // The job is not updated on the last firing as it is removed from the store following
    // it's last execution so the count will always be one less than the max firings.
    assertTrue(job.getExecutionCount() >= 9);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ByteSequence(org.apache.activemq.util.ByteSequence) Test(org.junit.Test)

Example 30 with ByteSequence

use of org.apache.activemq.util.ByteSequence in project activemq-artemis by apache.

the class JobSchedulerStoreTest method testRestart.

@Test(timeout = 120 * 1000)
public void testRestart() throws Exception {
    JobSchedulerStore store = new JobSchedulerStoreImpl();
    File directory = new File("target/test/ScheduledDB");
    IOHelper.mkdirs(directory);
    IOHelper.deleteChildren(directory);
    store.setDirectory(directory);
    final int NUMBER = 1000;
    store.start();
    List<ByteSequence> list = new ArrayList<>();
    for (int i = 0; i < NUMBER; i++) {
        ByteSequence buff = new ByteSequence(new String("testjob" + i).getBytes());
        list.add(buff);
    }
    JobScheduler js = store.getJobScheduler("test");
    js.startDispatching();
    int count = 0;
    long startTime = 10 * 60 * 1000;
    long period = startTime;
    for (ByteSequence job : list) {
        js.schedule("id:" + (count++), job, "", startTime, period, -1);
    }
    List<Job> test = js.getAllJobs();
    LOG.debug("Found {} jobs in the store before restart", test.size());
    assertEquals(list.size(), test.size());
    store.stop();
    store.start();
    js = store.getJobScheduler("test");
    test = js.getAllJobs();
    LOG.debug("Found {} jobs in the store after restart", test.size());
    assertEquals(list.size(), test.size());
    for (int i = 0; i < list.size(); i++) {
        String orig = new String(list.get(i).getData());
        String payload = new String(test.get(i).getPayload());
        assertEquals(orig, payload);
    }
}
Also used : ArrayList(java.util.ArrayList) JobSchedulerStoreImpl(org.apache.activemq.store.kahadb.scheduler.JobSchedulerStoreImpl) File(java.io.File) ByteSequence(org.apache.activemq.util.ByteSequence) Test(org.junit.Test)

Aggregations

ByteSequence (org.apache.activemq.util.ByteSequence)31 Test (org.junit.Test)13 CountDownLatch (java.util.concurrent.CountDownLatch)6 File (java.io.File)4 ActiveMQDestination (org.apache.activemq.command.ActiveMQDestination)4 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)3 ArrayList (java.util.ArrayList)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 ActiveMQMessage (org.apache.activemq.command.ActiveMQMessage)2 DataStructure (org.apache.activemq.command.DataStructure)2 MessageId (org.apache.activemq.command.MessageId)2 ProducerId (org.apache.activemq.command.ProducerId)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 Deflater (java.util.zip.Deflater)1 MessageConsumer (javax.jms.MessageConsumer)1 Session (javax.jms.Session)1 ActiveMQConnection (org.apache.activemq.ActiveMQConnection)1 ICoreMessage (org.apache.activemq.artemis.api.core.ICoreMessage)1