Hi,
I encounter a strange behaviour of the Queue object.
The following code is a simple POC for the problem I have.
import
java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import hudson.Extension;
import hudson.model.ManagementLink;
import hudson.model.Queue;
import hudson.security.Permission;
import jenkins.model.Jenkins;
@Extension
public class ManagedLink extends ManagementLink
{
private static final Logger logger =
Logger.getLogger(ManagedLink.class.getName());
private static ManagedLink instance;
Queue.Item[] items = null;
Queue queue;
private static final String URL = "samplelink";
private static final String ICON = "system-log-out.png";
public static ManagedLink getInstance()
{
List<ManagementLink> list =
Jenkins.getInstance().getManagementLinks();
for (ManagementLink link : list)
{
if (link instanceof ManagedLink)
{
instance = (ManagedLink)link;
break;
}
}
return instance;
}
@Override
public String getIconFileName()
{
return ICON;
}
@Override
public String getDisplayName()
{
return "ManagedLink";
}
@Override
public String getUrlName()
{
return URL;
}
@Override
public String getDescription()
{
return "Demo For Problem";
}
@Override
public Permission getRequiredPermission() {
return Jenkins.ADMINISTER;
}
Queue.Item[] getQueueItems()
{
if (items == null)
{
items = Queue.getInstance().getItems();
}
return items;
}
public synchronized void doIndex(StaplerRequest req,
StaplerResponse rsp) throws IOException
{
Jenkins.getInstance().checkPermission(getRequiredPermission());
items = Queue.getInstance().getItems();
logger.info("Queue length in doIndex: " + items.length);
queue = Queue.getInstance();
ExecutorService service =
Executors.newSingleThreadExecutor();
service.submit(new MyRunner());
rsp.sendRedirect2(req.getContextPath() + "/manage");
}
class MyRunner implements Runnable
{
@Override
public void run()
{
logger.info("Queue length in Runnable from queue
object from doIndex: " + queue.getItems().length);
logger.info("Queue length in Runnable with direct
access to Queue: "
+ Queue.getInstance().getItems().length);
logger.info("Queue length in Runnable with instance
variable direct access : " + items.length);
logger.info("Queue length in Runnable with instance
variable via method : "
+
ManagedLink.getInstance().getQueueItems().length);
}
}
}
Running this code in Jenkins on Windows, when there are entries
in the queue, I get the expected length.
But when I run this code inside my Linux VM, the Queue object is
returning 0 for the length:
Feb 05,
2017 12:16:02 AM INFO ManagedLink doIndex
Queue length in doIndex: 1
Feb 05, 2017 12:16:02 AM INFO ManagedLink$MyRunner run
Queue length in Runnable from queue object from doIndex: 0
Feb 05, 2017 12:16:02 AM INFO ManagedLink$MyRunner run
Queue length in Runnable with direct access to Queue: 0
Feb 05, 2017 12:16:02 AM INFO ManagedLink$MyRunner run
Queue length in Runnable with instance variable direct access : 1
Feb 05, 2017 12:16:02 AM INFO ManagedLink$MyRunner run
Queue length in Runnable with instance variable via method : 1
Strange thing is as well that when I run unit tests on Linux,
it works fine.
This happens on latest 2.32.2 Jenkins with openjdk 8 on Opensuse but
I can also observe this with a 1.609.3 Jenkins running with SAP JDK8
On Windows running with the JDK that comes with the Jenkins
installer (java 8).
Anyone has an idea what could be the reason for this?
Regards
Markus