1

I have a Spring bean which is exposed as JMX managed bean. Using JConsole I can invoke the methods (managed operations) and pass input paramters of primitive types and also String values. But not able to pass input parameter of type date. Can anyone help me to understand how pass an argument of type Date ?

Manu
  • 3,467
  • 3
  • 29
  • 28

1 Answers1

2

You have 2 basic choices, plus a few basic variations. Let's say you have a simple attribute like this:

import java.util.Date;
import java.text.SimpleDateFormat;
.......
public void setDate(Date date) {
    // Implement date function here
}

Your exposed JMX methods (which can be invoked through JConsole) which will internally create date and delegate to the above method would be:

Pass the date as a String with the format to parse as:

public void setDate(String format, String date) {
    try {
        setDate(new SimpleDateFormat(format).parse(date));
    } catch (ParseException e) {
        throw new RuntimeException("Failed to parse date [" + date + "] with expected format [" + format + "]", e);
    }
}

Variation: Use Standard Format

/** The standard date format to pass dates as  */
public static final String STD_FORMAT = "yyy-MM-dd";

public void setDate(String date) {
    try {
        setDate(new SimpleDateFormat(STD_FORMAT).parse(date));
    } catch (ParseException e) {
        throw new RuntimeException("Failed to parse date [" + date + "] with expected format [" + STD_FORMAT + "]", e);
    }       
}

Pass Date as UDT Long:

public void setDate(long time) {
    setDate(new Date(time));
}

Keep in mind that this is purely a JConsole limitation. Using JMX programmatically, you can pass a java.util.Date without issue.

Nicholas
  • 15,916
  • 4
  • 42
  • 66