Google
 
Web unafbapune.blogspot.com

Friday, February 15, 2008

 

Some JMX Quizzes

  1. Where can you find out all those Platform MBean Object Names ?
  2. How would you connect via JMX with username and password programmatically ?
Some possible answers:
  1. Where can you find out all those Platform MBean Object Names ?
  2. java.lang.management.ManagementFactory
  3. How would you connect via JMX with username and password programmatically ?
    Map<String,String[]> env = new HashMap<String,String[]>();
env.put(JMXConnector.CREDENTIALS, new String[]{"username", "password"});
final String url = "service:jmx:rmi://" + host + "/jndi/rmi://" + host + ":" + port + "/jmxrmi";
JMXServiceURL jmxServiceUrl = new JMXServiceURL(url);
JMXConnector conn = JMXConnectorFactory.connect(jmxServiceUrl, env);
MBeanServerConnection mbsc = conn.getMBeanServerConnection();
...

Wednesday, February 13, 2008

 

Some Quizzes on Bits, Bytes, etc.

  1. Given a byte, how would you turn the nth bit on ?
  2. Given a byte, how would you turn the nth bit off ?
  3. Given a long, how would you return 8 bytes (in big-endian order) containing the two's-complement binary representation of the long ?
  4. Given 8 bytes (in big-endian order) containing the two's-complement binary representation of a long, how would you return the long ?
  5. Given a UUID, how would you return a BigInteger representing the numeric value of the underlying 128-bit ?
Some possible answers:

(Don't peek if you want to give it a try!)

  1. Given a byte, how would you turn the nth bit on ?
        public static byte setBitOn(byte b, int n) {
    return (byte)(b | 1 << n);
    }
  2. Given a byte, how would you turn the nth bit off ?
        public static byte setBitOff(byte b, int n) {
    int mask = 0xFF - (1 << n);
    return (byte)(b & mask);
    }
  3. Given a long, how would you return 8 bytes (in big-endian order) containing the two's-complement binary representation of the long ?
        public static byte[] longTobytes(long val) {
    ByteBuffer bb = ByteBuffer.allocate(8);
    bb.putLong(val);
    return bb.array();
    }
  4. Given 8 bytes (in big-endian order) containing the two's-complement binary representation of a long, how would you return the long ?
        public static long bytesTolong(byte[] ba) {
    ByteBuffer bb = ByteBuffer.allocate(8);
    bb.put(ba);
    bb.flip();
    return bb.getLong();
    }
  5. Given a UUID, how would you return a BigInteger representing the numeric value of the underlying 128-bit ?
        public static BigInteger toBigInteger(UUID uuid) {
    long msb = uuid.getMostSignificantBits();
    long lsb = uuid.getLeastSignificantBits();
    ByteBuffer bb = ByteBuffer.allocate(16);
    bb.putLong(msb).putLong(lsb);
    return new BigInteger(bb.array());
    }

This page is powered by Blogger. Isn't yours?