I have written sample JMX MBean "PoolMBean" based on my uderstanding of MBeans. I have written this to manage and monitor the connection pool. My question here is, is this the way Mbeans are written? Are there any issues in this Mbean code not related to connection pool?
1) What kind of objects can an Mbean method return?
package pool;
import java.util.Date;
public class Connection {
public Date createdAt;
protected int usedCount;
protected boolean isAvailable = true;
public Connection newConnection(){
Connection con= null;
/**
* Code for creating Connection
*/
return con;
}
public void writeDate(){
/**
* Code to write data in the stream
*/
usedCount++;
}
}
package pool;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.management.MBeanServer;
import javax.management.ObjectName;
public class ConnectionPool {
public static int maxPoolSize = 20;
public int currentPoolSize = 10;
public LinkedList<Connection> totalPool = new LinkedList<Connection>();
public LinkedList<Connection> availablePool = new LinkedList<Connection>();
public static ConnectionPool cp = new ConnectionPool();
private ConnectionPool(){
}
public synchronized Connection getConnection(){
Connection con = null;
/**
*
*/
availablePool.remove(con);
con.isAvailable = false;
return con;
}
public synchronized void returnConnection(Connection con){
/**
*
*/
availablePool.addFirst(con);
con.isAvailable = true;
}
public static void main(String a[]){
try{
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
Pool mbean = new Pool();
ObjectName name = new ObjectName("test.conMbean:key1=Pool");
server.registerMBean(mbean, name);
System.out.println("Let me see out put");
System.in.read();
}catch (Exception e) {
e.printStackTrace();
}
}
}
package pool;
public interface PoolMBean {
public int getCurrentPoolSize();
public int getMaxPoolSize();
public void setMaxPoolSize(int maxSize);
}
package pool;
public class Pool implements PoolMBean {
@Override
public int getCurrentPoolSize() {
return ConnectionPool.cp.currentPoolSize;
}
@Override
public int getMaxPoolSize() {
return ConnectionPool.maxPoolSize;
}
@Override
public void setMaxPoolSize(int maxSize) {
ConnectionPool.maxPoolSize = maxSize;
}
}
Added this based on "yazan jber" answer provided below.
1) What kind of objects can a Mbean method return? For example if the PoolMBean
hasgetStatistics()
which returns totalPool LinkedList
object. In this case in JConsole
the value is displaying Unavailable
, but when I tried with HashMap
with String
objects it worked? So the JConsole
can't read everything, What it can read is my question here?
I have gone through the Oracle MXBean annotation API doc, the description here is bit complicated. What I got from this link is there are OpenType,ArrayType
, CompositeType
, SimpleType
and TabularType
these deals with only
java.lang.Void
,java.lang.Boolean
,java.lang.Character
,java.lang.Byte
,java.lang.Short
,java.lang.Integer
,java.lang.Long
,java.lang.Float
,java.lang.Double
,java.lang.String
,java.math.BigDecimal
,java.math.BigInteger
,java.util.Date
,javax.management.ObjectName
,CompositeData.class.getName()
,TabularData.class.getName()
these objects. MBean
should return any one of this OpenType
.
If we want to return any other type that new type should implement CompositeData
interface, I didn't get much how this implementation will help Jconsole to read open objects, it is another complicated question?
To Track individual components in my application we should have our own MBeans? If my understanding is right I can use simple java class for this purpose, the extra benefit I'm getting here is the JConsole UI, Isn't it?