• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 12828940400

17 Jan 2025 12:20PM UTC coverage: 63.78% (-0.002%) from 63.782%
12828940400

push

github

openmrs-bot
Setting new SNAPSHOT version

21708 of 34036 relevant lines covered (63.78%)

0.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.86
/api/src/main/java/org/openmrs/scheduler/tasks/TaskThreadedInitializationWrapper.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.scheduler.tasks;
11

12
import java.util.concurrent.locks.Condition;
13
import java.util.concurrent.locks.Lock;
14
import java.util.concurrent.locks.ReentrantLock;
15

16
import org.openmrs.scheduler.Task;
17
import org.openmrs.scheduler.TaskDefinition;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

21
/**
22
 * This class executes the Task.initialize method in a new thread. Extend this class if you want
23
 * your {@link #initialize(TaskDefinition)} method to run in a new thread (and hence not hold up the
24
 * "startup" processes)
25
 */
26
public class TaskThreadedInitializationWrapper implements Task {
27
        
28
        // Logger 
29
        private static final Logger log = LoggerFactory.getLogger(TaskThreadedInitializationWrapper.class);
1✔
30
        
31
        private Task task;
32
        
33
        private boolean initialized = false;
1✔
34
        
35
        private final Lock lock = new ReentrantLock();
1✔
36
        
37
        private final Condition initializedCond = lock.newCondition();
1✔
38
        
39
        /**
40
         * Default constructor to create this wrapper
41
         * 
42
         * @param task the Task to wrap around
43
         */
44
        public TaskThreadedInitializationWrapper(Task task) {
1✔
45
                this.task = task;
1✔
46
        }
1✔
47
        
48
        /**
49
         * @see org.openmrs.scheduler.Task#execute() Executes the task defined in the task definition
50
         *      but waits until the initialize method has finished
51
         */
52
        @Override
53
        public void execute() {
54
                lock.lock();
1✔
55
                try {
56
                        while (!initialized) {
1✔
57
                                initializedCond.await();
×
58
                        }
59
                }
60
                catch (InterruptedException e) {
×
61
                        log.error("Task could not be initialized hence not be executed.", e);
×
62
                        return;
×
63
                }
64
                finally {
65
                        lock.unlock();
1✔
66
                }
67
                
68
                task.execute();
1✔
69
        }
1✔
70
        
71
        /**
72
         * @see org.openmrs.scheduler.Task#initialize(org.openmrs.scheduler.TaskDefinition) Initializes
73
         *      the task and sets the task definition. This method is non-blocking by executing in a new
74
         *      thread.
75
         */
76
        @Override
77
        public void initialize(final TaskDefinition config) {
78
                Runnable r = () -> {
1✔
79
                        lock.lock();
1✔
80
                        try {
81
                                task.initialize(config);
1✔
82
                                initialized = true;
1✔
83
                                initializedCond.signalAll();
1✔
84
                        }
85
                        finally {
86
                                lock.unlock();
1✔
87
                        }
88
                };
1✔
89
                
90
                new Thread(r).start();
1✔
91
        }
1✔
92
        
93
        /**
94
         * @see org.openmrs.scheduler.Task#getTaskDefinition()
95
         */
96
        @Override
97
        public TaskDefinition getTaskDefinition() {
98
                return task != null ? task.getTaskDefinition() : null;
1✔
99
        }
100
        
101
        /**
102
         * @see org.openmrs.scheduler.Task#isExecuting()
103
         */
104
        @Override
105
        public boolean isExecuting() {
106
                return task.isExecuting();
×
107
        }
108
        
109
        /**
110
         * @see org.openmrs.scheduler.Task#shutdown()
111
         */
112
        @Override
113
        public void shutdown() {
114
                task.shutdown();
×
115
        }
×
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc