#StackBounty: #java #multithreading #thread-safety #concurrency #hash-map Modify map only from a single thread every X seconds and then…

Bounty: 50

I have a class in which I am populating a map liveSocketsByDatacenter from a single background thread every 30 seconds inside updateLiveSockets() method and then I have a method getNextSocket() which will be called by multiple reader threads to get a live socket available which uses the same map to get this info.

public class SocketManager {
  private static final Random random = new Random();
  private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  private final AtomicReference<Map<Datacenters, List<SocketHolder>>> liveSocketsByDatacenter =
      new AtomicReference<>(Collections.unmodifiableMap(new HashMap<>()));
  private final ZContext ctx = new ZContext();

  // Lazy Loaded Singleton Pattern
  private static class Holder {
    private static final SocketManager instance = new SocketManager();
  }

  public static SocketManager getInstance() {
    return Holder.instance;
  }

  private SocketManager() {
    connectToZMQSockets();
    scheduler.scheduleAtFixedRate(new Runnable() {
      public void run() {
        updateLiveSockets();
      }
    }, 30, 30, TimeUnit.SECONDS);
  }

  private void connectToZMQSockets() {
    Map<Datacenters, ImmutableList<String>> socketsByDatacenter = Utils.SERVERS;
    // The map in which I put all the live sockets
    Map<Datacenters, List<SocketHolder>> updatedLiveSocketsByDatacenter = new HashMap<>();
    for (Map.Entry<Datacenters, ImmutableList<String>> entry : socketsByDatacenter.entrySet()) {
      List<SocketHolder> addedColoSockets = connect(entry.getKey(), entry.getValue(), ZMQ.PUSH);
      updatedLiveSocketsByDatacenter.put(entry.getKey(),
          Collections.unmodifiableList(addedColoSockets));
    }
    // Update the map content
    this.liveSocketsByDatacenter.set(Collections.unmodifiableMap(updatedLiveSocketsByDatacenter));
  }

  private List<SocketHolder> connect(Datacenters colo, List<String> addresses, int socketType) {
    List<SocketHolder> socketList = new ArrayList<>();
    for (String address : addresses) {
      try {
        Socket client = ctx.createSocket(socketType);
        // Set random identity to make tracing easier
        String identity = String.format("%04X-%04X", random.nextInt(), random.nextInt());
        client.setIdentity(identity.getBytes(ZMQ.CHARSET));
        client.setTCPKeepAlive(1);
        client.setSendTimeOut(7);
        client.setLinger(0);
        client.connect(address);

        SocketHolder zmq = new SocketHolder(client, ctx, address, true);
        socketList.add(zmq);
      } catch (Exception ex) {
        // log error
      }
    }
    return socketList;
  }

  // this method will be called by multiple threads to get the next live socket
  public Optional<SocketHolder> getNextSocket() {
    // For the sake of consistency make sure to use the same map instance
    // in the whole implementation of my method by getting my entries
    // from the local variable instead of the member variable
    Map<Datacenters, List<SocketHolder>> liveSocketsByDatacenter =
        this.liveSocketsByDatacenter.get();
    Optional<SocketHolder> liveSocket = Optional.absent();
    List<Datacenters> dcs = Datacenters.getOrderedDatacenters();
    for (Datacenters dc : dcs) {
      liveSocket = getLiveSocket(liveSocketsByDatacenter.get(dc));
      if (liveSocket.isPresent()) {
        break;
      }
    }
    return liveSocket;
  }

  private Optional<SocketHolder> getLiveSocket(final List<SocketHolder> listOfEndPoints) {
    if (!CollectionUtils.isEmpty(listOfEndPoints)) {
      // The list of live sockets
      List<SocketHolder> liveOnly = new ArrayList<>(listOfEndPoints.size());
      for (SocketHolder obj : listOfEndPoints) {
        if (obj.isLive()) {
          liveOnly.add(obj);
        }
      }
      if (!liveOnly.isEmpty()) {
        // The list is not empty so we shuffle it an return the first element
        Collections.shuffle(liveOnly);
        return Optional.of(liveOnly.get(0));
      }
    }
    return Optional.absent();
  }

  // Added the modifier synchronized to prevent concurrent modification
  // it is needed because to build the new map we first need to get the
  // old one so both must be done atomically to prevent concistency issues
  private synchronized void updateLiveSockets() {
    Map<Datacenters, ImmutableList<String>> socketsByDatacenter = Utils.SERVERS;

    // Initialize my new map with the current map content
    Map<Datacenters, List<SocketHolder>> liveSocketsByDatacenter =
        new HashMap<>(this.liveSocketsByDatacenter.get());

    for (Entry<Datacenters, ImmutableList<String>> entry : socketsByDatacenter.entrySet()) {
      List<SocketHolder> liveSockets = liveSocketsByDatacenter.get(entry.getKey());
      List<SocketHolder> liveUpdatedSockets = new ArrayList<>();
      for (SocketHolder liveSocket : liveSockets) {
        Socket socket = liveSocket.getSocket();
        String endpoint = liveSocket.getEndpoint();
        Map<byte[], byte[]> holder = populateMap();

        boolean status = SendToSocket.getInstance().execute(3, holder, socket);
        boolean isLive = (status) ? true : false;
        SocketHolder zmq = new SocketHolder(socket, liveSocket.getContext(), endpoint, isLive);
        liveUpdatedSockets.add(zmq);
      }
      liveSocketsByDatacenter.put(entry.getKey(),
          Collections.unmodifiableList(liveUpdatedSockets));
    }
    this.liveSocketsByDatacenter.set(Collections.unmodifiableMap(liveSocketsByDatacenter));
  }
}

As you can see in my class:

  • From a single background thread which runs every 30 seconds, I populate liveSocketsByDatacenter map with all the live sockets in updateLiveSockets() method.
  • And then from multiple threads, I call the getNextSocket() method to give me a live socket available which uses a liveSocketsByDatacenter map to get the required information.

I have my code working fine without any issues and wanted to see if there is any better and efficient way to write this. I also wanted to get an opinion on thread safety or any race condition if it is there, but so far I haven’t seen any issues. Maybe I don’t need a synchronized modifier on my updateLiveSockets() method.


Get this bounty!!!

#StackBounty: #java #jmx #tomcat8 Why can't I get JMX to run with Tomcat8 through CATALINA_OPTS?

Bounty: 50

According to https://tomcat.apache.org/tomcat-8.0-doc/monitoring.html#Introduction I should be able to active JMX by setting some env variables on CATALINA_OPTS. I’ve done this:

export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"

Some places seem to suggest I need catalina-jmx-remote.jar in $CATALINA_OPTS/lib which I have:

$ ls -l /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar
-rw-r--r-- 1 root root 13608 Jan 18 23:05 /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar

I’ve also made sure the environment variable is properly set:

$ sudo cat /proc/20893/environ
LANG=en_USLANGUAGE=en_US:PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOME=/srv/tomcatLOGNAME=tomcatUSER=tomcatSHELL=TOMCAT_JAVA_HOME=/opt/java/oracle/jdk1.8.0_112CATALINA_HOME=/opt/tomcat/apache-tomcat-8.0.37CATALINA_BASE=/srv/tomcat/catalina/tomcatCATALINA_OPTS=-Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

Despite this, JMX is not listening on 1099 and I cannot connect to it:

$ netstat --listen | grep 1099

If I run a regular java program with:

$ java -Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false HelloWorld

This also seems to work if I pass it as args when starting tomcat:

/opt/java/oracle/jdk1.8.0_112/bin/java 
  -Djava.util.logging.config.file=/srv/tomcat/catalina/%i/conf/logging.properties 
  -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
  -Djava.security.egd=file:/dev/./urandom 
  -Djava.endorsed.dirs=/opt/tomcat/apache-tomcat-8.0.37/endorsed 
  -Dcatalina.home=/opt/tomcat/apache-tomcat-8.0.37 
  -Dcatalina.base=/srv/tomcat/catalina/%i 
  -Djava.io.tmpdir=/srv/tomcat/catalina/%i/temp 
  -Djava.net.preferIPv4Stack=true 
  -Djava.rmi.server.hostname=172.16.29.134 
  -Dcom.sun.management.jmxremote.authenticate=false 
  -Dcom.sun.management.jmxremote.port=1099 
  -Dcom.sun.management.jmxremote.ssl=false 
  -classpath "/opt/tomcat/apache-tomcat-8.0.37/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-8.0.37/bin/tomcat-juli.jar" 
  org.apache.catalina.startup.Bootstrap start

So why doesn’t CATALINA_OPTS get picked up like the documentation suggests? What is it I’m missing?


Get this bounty!!!

#StackBounty: #java #jmx #tomcat8 Why can't I get JMX to run with Tomcat8 through CATALINA_OPTS?

Bounty: 50

According to https://tomcat.apache.org/tomcat-8.0-doc/monitoring.html#Introduction I should be able to active JMX by setting some env variables on CATALINA_OPTS. I’ve done this:

export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"

Some places seem to suggest I need catalina-jmx-remote.jar in $CATALINA_OPTS/lib which I have:

$ ls -l /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar
-rw-r--r-- 1 root root 13608 Jan 18 23:05 /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar

I’ve also made sure the environment variable is properly set:

$ sudo cat /proc/20893/environ
LANG=en_USLANGUAGE=en_US:PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOME=/srv/tomcatLOGNAME=tomcatUSER=tomcatSHELL=TOMCAT_JAVA_HOME=/opt/java/oracle/jdk1.8.0_112CATALINA_HOME=/opt/tomcat/apache-tomcat-8.0.37CATALINA_BASE=/srv/tomcat/catalina/tomcatCATALINA_OPTS=-Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

Despite this, JMX is not listening on 1099 and I cannot connect to it:

$ netstat --listen | grep 1099

If I run a regular java program with:

$ java -Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false HelloWorld

This also seems to work if I pass it as args when starting tomcat:

/opt/java/oracle/jdk1.8.0_112/bin/java 
  -Djava.util.logging.config.file=/srv/tomcat/catalina/%i/conf/logging.properties 
  -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
  -Djava.security.egd=file:/dev/./urandom 
  -Djava.endorsed.dirs=/opt/tomcat/apache-tomcat-8.0.37/endorsed 
  -Dcatalina.home=/opt/tomcat/apache-tomcat-8.0.37 
  -Dcatalina.base=/srv/tomcat/catalina/%i 
  -Djava.io.tmpdir=/srv/tomcat/catalina/%i/temp 
  -Djava.net.preferIPv4Stack=true 
  -Djava.rmi.server.hostname=172.16.29.134 
  -Dcom.sun.management.jmxremote.authenticate=false 
  -Dcom.sun.management.jmxremote.port=1099 
  -Dcom.sun.management.jmxremote.ssl=false 
  -classpath "/opt/tomcat/apache-tomcat-8.0.37/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-8.0.37/bin/tomcat-juli.jar" 
  org.apache.catalina.startup.Bootstrap start

So why doesn’t CATALINA_OPTS get picked up like the documentation suggests? What is it I’m missing?


Get this bounty!!!

#StackBounty: #java #jmx #tomcat8 Why can't I get JMX to run with Tomcat8 through CATALINA_OPTS?

Bounty: 50

According to https://tomcat.apache.org/tomcat-8.0-doc/monitoring.html#Introduction I should be able to active JMX by setting some env variables on CATALINA_OPTS. I’ve done this:

export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"

Some places seem to suggest I need catalina-jmx-remote.jar in $CATALINA_OPTS/lib which I have:

$ ls -l /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar
-rw-r--r-- 1 root root 13608 Jan 18 23:05 /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar

I’ve also made sure the environment variable is properly set:

$ sudo cat /proc/20893/environ
LANG=en_USLANGUAGE=en_US:PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOME=/srv/tomcatLOGNAME=tomcatUSER=tomcatSHELL=TOMCAT_JAVA_HOME=/opt/java/oracle/jdk1.8.0_112CATALINA_HOME=/opt/tomcat/apache-tomcat-8.0.37CATALINA_BASE=/srv/tomcat/catalina/tomcatCATALINA_OPTS=-Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

Despite this, JMX is not listening on 1099 and I cannot connect to it:

$ netstat --listen | grep 1099

If I run a regular java program with:

$ java -Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false HelloWorld

This also seems to work if I pass it as args when starting tomcat:

/opt/java/oracle/jdk1.8.0_112/bin/java 
  -Djava.util.logging.config.file=/srv/tomcat/catalina/%i/conf/logging.properties 
  -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
  -Djava.security.egd=file:/dev/./urandom 
  -Djava.endorsed.dirs=/opt/tomcat/apache-tomcat-8.0.37/endorsed 
  -Dcatalina.home=/opt/tomcat/apache-tomcat-8.0.37 
  -Dcatalina.base=/srv/tomcat/catalina/%i 
  -Djava.io.tmpdir=/srv/tomcat/catalina/%i/temp 
  -Djava.net.preferIPv4Stack=true 
  -Djava.rmi.server.hostname=172.16.29.134 
  -Dcom.sun.management.jmxremote.authenticate=false 
  -Dcom.sun.management.jmxremote.port=1099 
  -Dcom.sun.management.jmxremote.ssl=false 
  -classpath "/opt/tomcat/apache-tomcat-8.0.37/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-8.0.37/bin/tomcat-juli.jar" 
  org.apache.catalina.startup.Bootstrap start

So why doesn’t CATALINA_OPTS get picked up like the documentation suggests? What is it I’m missing?


Get this bounty!!!

#StackBounty: #java #jmx #tomcat8 Why can't I get JMX to run with Tomcat8 through CATALINA_OPTS?

Bounty: 50

According to https://tomcat.apache.org/tomcat-8.0-doc/monitoring.html#Introduction I should be able to active JMX by setting some env variables on CATALINA_OPTS. I’ve done this:

export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"

Some places seem to suggest I need catalina-jmx-remote.jar in $CATALINA_OPTS/lib which I have:

$ ls -l /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar
-rw-r--r-- 1 root root 13608 Jan 18 23:05 /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar

I’ve also made sure the environment variable is properly set:

$ sudo cat /proc/20893/environ
LANG=en_USLANGUAGE=en_US:PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOME=/srv/tomcatLOGNAME=tomcatUSER=tomcatSHELL=TOMCAT_JAVA_HOME=/opt/java/oracle/jdk1.8.0_112CATALINA_HOME=/opt/tomcat/apache-tomcat-8.0.37CATALINA_BASE=/srv/tomcat/catalina/tomcatCATALINA_OPTS=-Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

Despite this, JMX is not listening on 1099 and I cannot connect to it:

$ netstat --listen | grep 1099

If I run a regular java program with:

$ java -Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false HelloWorld

This also seems to work if I pass it as args when starting tomcat:

/opt/java/oracle/jdk1.8.0_112/bin/java 
  -Djava.util.logging.config.file=/srv/tomcat/catalina/%i/conf/logging.properties 
  -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
  -Djava.security.egd=file:/dev/./urandom 
  -Djava.endorsed.dirs=/opt/tomcat/apache-tomcat-8.0.37/endorsed 
  -Dcatalina.home=/opt/tomcat/apache-tomcat-8.0.37 
  -Dcatalina.base=/srv/tomcat/catalina/%i 
  -Djava.io.tmpdir=/srv/tomcat/catalina/%i/temp 
  -Djava.net.preferIPv4Stack=true 
  -Djava.rmi.server.hostname=172.16.29.134 
  -Dcom.sun.management.jmxremote.authenticate=false 
  -Dcom.sun.management.jmxremote.port=1099 
  -Dcom.sun.management.jmxremote.ssl=false 
  -classpath "/opt/tomcat/apache-tomcat-8.0.37/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-8.0.37/bin/tomcat-juli.jar" 
  org.apache.catalina.startup.Bootstrap start

So why doesn’t CATALINA_OPTS get picked up like the documentation suggests? What is it I’m missing?


Get this bounty!!!

#StackBounty: #java #jmx #tomcat8 Why can't I get JMX to run with Tomcat8 through CATALINA_OPTS?

Bounty: 50

According to https://tomcat.apache.org/tomcat-8.0-doc/monitoring.html#Introduction I should be able to active JMX by setting some env variables on CATALINA_OPTS. I’ve done this:

export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"

Some places seem to suggest I need catalina-jmx-remote.jar in $CATALINA_OPTS/lib which I have:

$ ls -l /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar
-rw-r--r-- 1 root root 13608 Jan 18 23:05 /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar

I’ve also made sure the environment variable is properly set:

$ sudo cat /proc/20893/environ
LANG=en_USLANGUAGE=en_US:PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOME=/srv/tomcatLOGNAME=tomcatUSER=tomcatSHELL=TOMCAT_JAVA_HOME=/opt/java/oracle/jdk1.8.0_112CATALINA_HOME=/opt/tomcat/apache-tomcat-8.0.37CATALINA_BASE=/srv/tomcat/catalina/tomcatCATALINA_OPTS=-Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

Despite this, JMX is not listening on 1099 and I cannot connect to it:

$ netstat --listen | grep 1099

If I run a regular java program with:

$ java -Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false HelloWorld

This also seems to work if I pass it as args when starting tomcat:

/opt/java/oracle/jdk1.8.0_112/bin/java 
  -Djava.util.logging.config.file=/srv/tomcat/catalina/%i/conf/logging.properties 
  -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
  -Djava.security.egd=file:/dev/./urandom 
  -Djava.endorsed.dirs=/opt/tomcat/apache-tomcat-8.0.37/endorsed 
  -Dcatalina.home=/opt/tomcat/apache-tomcat-8.0.37 
  -Dcatalina.base=/srv/tomcat/catalina/%i 
  -Djava.io.tmpdir=/srv/tomcat/catalina/%i/temp 
  -Djava.net.preferIPv4Stack=true 
  -Djava.rmi.server.hostname=172.16.29.134 
  -Dcom.sun.management.jmxremote.authenticate=false 
  -Dcom.sun.management.jmxremote.port=1099 
  -Dcom.sun.management.jmxremote.ssl=false 
  -classpath "/opt/tomcat/apache-tomcat-8.0.37/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-8.0.37/bin/tomcat-juli.jar" 
  org.apache.catalina.startup.Bootstrap start

So why doesn’t CATALINA_OPTS get picked up like the documentation suggests? What is it I’m missing?


Get this bounty!!!

#StackBounty: #java #jmx #tomcat8 Why can't I get JMX to run with Tomcat8 through CATALINA_OPTS?

Bounty: 50

According to https://tomcat.apache.org/tomcat-8.0-doc/monitoring.html#Introduction I should be able to active JMX by setting some env variables on CATALINA_OPTS. I’ve done this:

export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"

Some places seem to suggest I need catalina-jmx-remote.jar in $CATALINA_OPTS/lib which I have:

$ ls -l /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar
-rw-r--r-- 1 root root 13608 Jan 18 23:05 /opt/tomcat/apache-tomcat-8.0.37/lib/catalina-jmx-remote.jar

I’ve also made sure the environment variable is properly set:

$ sudo cat /proc/20893/environ
LANG=en_USLANGUAGE=en_US:PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOME=/srv/tomcatLOGNAME=tomcatUSER=tomcatSHELL=TOMCAT_JAVA_HOME=/opt/java/oracle/jdk1.8.0_112CATALINA_HOME=/opt/tomcat/apache-tomcat-8.0.37CATALINA_BASE=/srv/tomcat/catalina/tomcatCATALINA_OPTS=-Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

Despite this, JMX is not listening on 1099 and I cannot connect to it:

$ netstat --listen | grep 1099

If I run a regular java program with:

$ java -Djava.rmi.server.hostname=172.16.29.134 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false HelloWorld

This also seems to work if I pass it as args when starting tomcat:

/opt/java/oracle/jdk1.8.0_112/bin/java 
  -Djava.util.logging.config.file=/srv/tomcat/catalina/%i/conf/logging.properties 
  -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
  -Djava.security.egd=file:/dev/./urandom 
  -Djava.endorsed.dirs=/opt/tomcat/apache-tomcat-8.0.37/endorsed 
  -Dcatalina.home=/opt/tomcat/apache-tomcat-8.0.37 
  -Dcatalina.base=/srv/tomcat/catalina/%i 
  -Djava.io.tmpdir=/srv/tomcat/catalina/%i/temp 
  -Djava.net.preferIPv4Stack=true 
  -Djava.rmi.server.hostname=172.16.29.134 
  -Dcom.sun.management.jmxremote.authenticate=false 
  -Dcom.sun.management.jmxremote.port=1099 
  -Dcom.sun.management.jmxremote.ssl=false 
  -classpath "/opt/tomcat/apache-tomcat-8.0.37/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-8.0.37/bin/tomcat-juli.jar" 
  org.apache.catalina.startup.Bootstrap start

So why doesn’t CATALINA_OPTS get picked up like the documentation suggests? What is it I’m missing?


Get this bounty!!!

#StackBounty: #java #android #drag-and-drop #recyclerview Why do the list elements not swap?

Bounty: 50

I have a RecyclerView and have implemented an onMove command called onItemMove. I’m using onItemMove to try and get the list elements to swap position when dragged around, but the list elements just hover over each, they don’t swap. How can I correct this?

Note 1: onItemDismiss works fine; it swipes the item away and removes it from the list.

Note 2: I’ve tried to Override onItemMove, but it doesn’t actually override its superclass.

List Adaptor Class: This contains the onItemMove command

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
    private static final String TAG = "ListAdapter";
    Context context;
    private List<UserData> dataList = new ArrayList<>();
    LayoutInflater inflater;
    Listener listener;
    DbHelper dbHelper;



    public interface Listener {
        void nameToChnge(String name);
    }

    public ListAdapter(Context context, List<UserData> dataList1) {
        this.context = context;
        this.dataList = dataList1;
        this.listener= (Listener) context;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = inflater.inflate(R.layout.recylerview_one, parent, false);
        ListViewHolder viewHolder = new ListViewHolder(convertView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ListViewHolder holder, final int position) {
        holder.tv_name.setText(dataList.get(position).name);
        holder.tv_quantity.setText(dataList.get(position).quantity);
        holder.tv_description.setText(dataList.get(position).description + "");

        holder.relLayout.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String s = dataList.get(position).id;
                Integer stringo = Integer.parseInt(s);
                Intent intent = new Intent(context, ItemEditActivity.class);
                intent.putExtra("ItemNumber", stringo);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class ListViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name, tv_quantity, tv_description;
        RelativeLayout relLayout;


        public ListViewHolder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.nameDisplay);
            tv_quantity = (TextView) itemView.findViewById(R.id.quantityDisplay);
            tv_description = (TextView) itemView.findViewById(R.id.descriptionDisplay);
            relLayout = (RelativeLayout) itemView.findViewById(R.id.relLayout);
        }
    }



    public void onItemDismiss(final int position) {
        dataList.remove(position);
        notifyItemRemoved(position);

    }

    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(dataList, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);

    }
}

ItemTouchHelper Class:

  public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback{
        private final ListAdapter  mAdapter;


        public SimpleItemTouchHelperCallback(ListAdapter adapter) {
            mAdapter = adapter;
        }

        @Ov

erride
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView,
                          RecyclerView.ViewHolder viewHolder,
                          RecyclerView.ViewHolder target) {
        mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());

        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
    }
}

Part of the Main Activity where I initialise the RecyclerView and
SimpleItemTouchHelperCallback:

public class MainActivity extends AppCompatActivity implements ListAdapter.Listener {
    private static final String TAG = "MainActivity";

    RecyclerView recyclerView;
    DbHelper dbHelper;
    ListAdapter adapter;
    FloatingActionButton fab;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        introItem();
        dbHelper = DbHelper.getInstance(getApplicationContext());

        recyclerView= (RecyclerView) findViewById(R.id.rv_contactlist);
        adapter = new ListAdapter(this, dbHelper.getAllUser());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        ItemTouchHelper.Callback callback =
                new SimpleItemTouchHelperCallback(adapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        touchHelper.attachToRecyclerView(recyclerView);

        totalQuantity();

        adapter.notifyDataSetChanged();
        fabHideShow();
        versionCheckMethod();

    }


Get this bounty!!!

#StackBounty: #java #xserver Java programs cause my xserver to freeze

Bounty: 100

I’m running Ubuntu 15.10. Sometimes running Java programs causes my entire xserver to freeze. I can’t close stuff, use programs, or do stuff like type in terminal. Using Control+Alt+F1 allows me to kill the Java process to unfreeze the xserver.

Looking at the Java process, it’s not using a huge amount of CPU (1-3%) or RAM (50MB-100MB).
My java version:

paul@paulpc:~$ java -version
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

However, running Java programs with taskset 0x80 <program> seems to make the programs not crash the xserver, however they run much slower. An example program that causes this is Netbeans.

Let me know if I need to provide any more details.


Get this bounty!!!

#StackBounty: #16.04 #java #eclipse #openjdk Problem launching eclipse in Ubuntu 16.04

Bounty: 50

I am running Ubuntu 16.04 I have installed openJDK via synaptic and also eclipse via synaptic. Everything was working fine. However suddenly I am getting this error when I try to open eclipse.

JVM terminated. Exit code=1
/usr/bin/java
-Xms40m
-Xmx384m
-Dorg.eclipse.equinox.p2.reconciler.dropins.directory=/usr/share/eclipse/dropins
-XX:MaxPermSize=256m
-jar     /usr/lib/eclipse//plugins/org.eclipse.equinox.launcher_1.3.0.dist.jar
-os linux
-ws gtk
-arch x86_64
-showsplash     /usr/lib/eclipse//plugins/org.eclipse.platform_3.8.1.dist/splash.bmp
-launcher /usr/lib/eclipse/eclipse
-name Eclipse
--launcher.library     /usr/lib/eclipse//plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1    .1.200.dist/eclipse_1503.so
-startup     /usr/lib/eclipse//plugins/org.eclipse.equinox.launcher_1.3.0.dist.jar
--launcher.overrideVmargs
-exitdata fc8023
-vm /usr/bin/java
-vmargs
-Xms40m
-Xmx384m
-    Dorg.eclipse.equinox.p2.reconciler.dropins.directory=/usr/share/eclipse/d    ropins
-XX:MaxPermSize=256m
-jar     /usr/lib/eclipse//plugins/org.eclipse.equinox.launcher_1.3.0.dist.jar 

How to fix this?


Get this bounty!!!