- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理一个已构建 MBean 的代码库(用于导出到 jmx)。
原始代码只是构建一个 MBeanInfo 实例:
@Override
public MBeanInfo getMBeanInfo() {
MBeanAttributeInfo[] attrs = //SLOW TO BUILD
return new MBeanInfo(...attrs...);
}
由于构建 mbean 属性的成本很高,并且该方法被调用得相当频繁(即使没有附加 jmx 客户端),因此我尝试创建 MBeanInto 的子类来延迟计算这些属性:
public class LazyMBeanInfo extends MBeanInfo implements Externalizable {
private transient AttributeCallback callback = null;
private volatile MBeanAttributeInfo[] lazyAttrs = null;
public LazyMBeanInfo(...AttributeCallback callback...) throws IllegalArgumentException {
super(className, description, null, constructors, operations, notifications);
this.callback = callback;
}
@Override
public MBeanAttributeInfo[] getAttributes() {
MBeanAttributeInfo[] val = lazyAttrs;
if (val != null) {
return val.clone(); //match upstream behaviour
}
if (callback == null) {
throw new IllegalStateException("BUG");
}
val = callback.buildAttributes();
if (val == null) {
val = new MBeanAttributeInfo[0];
}
lazyAttrs = val;
return val.clone();
}
public interface AttributeCallback {
MBeanAttributeInfo[] buildAttributes();
}
}
问题是 JMX(通过 RMI)序列化 MBeanInfo 对象,然后在 jconsole(或 jvisualVM)中出现错误:
那么 - 我可以以某种方式实现Externalized并将自己序列化为父类的实例吗?理想情况下,我希望它能够工作:
public class LazyMBeanInfo extends MBeanInfo implements Externalizable {
//same as before, plus:
@Override
public void writeExternal(ObjectOutput out) throws IOException {
MBeanInfo vanilla = new MBeanInfo(...);
out.writeObject(vanilla);
}
}
但事实并非如此。
这可能吗?
最佳答案
除非您使用[高度动态] DynamicMBeans,否则我不明白为什么每次调用 getMBeanInfo()
时都需要重建 MBeanInfo,但是......
您的 LazyMBeanInfo 可以正常工作(尽管我尚未测试此特定情况)。 MBeanInfo 已经实现了 Serialized,因此您想要的是序列化过程写出 MBeanInfo,而不是 LazyMBeanInfo,因为客户端的类路径中可能没有该类。然而,LazyMBeanInfo可以实现这个方法:
Object writeReplace() throws ObjectStreamException;
此时您将写出底层 MBeanInfo。请参阅Serializable JavaDoc,具体来说:
Serializable classes that need to designate an alternative object to be used when writing an object to the stream should implement this special method with the exact signature:
ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
这样,实际的对象可以是 LazyMBeanInfo 的实例,但您写出的内容可以是实际的 MBeanInfo,它是根据缓存的lazyAttrs 构建的。
话虽如此,我不会实现首次调用时构建的方法,而是通过在首次创建 MBean 或注册 MBean 时简单地构建完整的 MBeanInfo 来实现首次使用前构建。然后只需在每次 getMBeanInfo()
调用时返回预构建的 MBeanInfo。
要在 MBean 注册时执行此操作,请实现 MBeanRegistration接口(interface),并在 postRegister 中构建缓存的 MBeanInfo方法。
关于java - 让子类序列化为父类(super class)的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50768567/
问题比较Java,但是我想在Android中实现: 假设有 3 个或更多类相互扩展: class A { ... int color; ... } class B extend
我知道标题听起来有点奇怪,但这正是我愿意做的。简单解释:A类是B类的子类,B类也是C类的子类>. 现在,所有这些类都包含方法m()。在我的 A 类中,这是我唯一可以访问的类,因为其他类仅在运行时可用,
我有一个 UIViewController 类 A 和 B。 A 使用以下方式加载 B:[A.view addSubView B.view]。 B 有一个带有“后退”按钮的导航栏。我想在单击时返回到
我有以下(第三方)类结构。我们将调用第三方项目 ProjectSeriously,并注意我使用 System.out.println 代替其他复杂的功能(100 行代码) . class A {
在下面的代码中,我从 Game 扩展了 MyGame。我有两个问题: 我们是否需要为所有render()、dispose()、pause()调用super方法 和 resize(w,h)?很多人都没有
例如,假设我想在调用 super.viewDidLoad() 时跳过一级。所以我希望能够做这样的事情: override func viewDidLoad() { super.super.vi
public class Faculty extends Employee { public static void main(String[] args) { new Fac
假设我有: class Superclass { //fields... methodA() {...} methodB() {...} ... } class Subclass exte
这个问题在这里已经有了答案: Why is super.super.method(); not allowed in Java? (22 个答案) 关闭 9 年前。 我怀疑我想做的事情是否可行。我有
我有一个实现 Initializable 的类。 public abstract class ExampleClass implements Initializable { public vo
我想知道,我有这个大数组,是否可以只在内存中使用一次而不是每个线程一次?以 stackoverflow 上的标签为例。他们几乎从不改变,为什么不为他们留下一个内存点呢?甚至可能将该数组永久保存在内存中
假设这三个类具有这个简单的层次结构: class A { func foo() { print("A") } } class B: A { override fu
有没有办法在 TypeScript 中调用 super.super.methodName。我想避免调用super.methodName,但我想调用二祖的methodName方法。 谢谢。 最佳答案 T
这个问题已经有答案了: When do I use super()? (11 个回答) 已关闭 7 年前。 package Geometry; public abstract class Geomet
我必须执行and()在我的实现 Predicate 的业务对象上. 出现问题的代码是 and() 行调用: Predicate predicate = new M
我有一个实现接口(interface)的抽象父类(super class): public abstract class FooMatrix implements Matrix { publi
我有四个 UIView:viewA 是 Root View ,它有 viewB 作为它的 subview 。 viewB 将 viewC 作为其 subview ,而 viewC 将 viewD 作为
有什么区别: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() 和:
我有一个通用接口(interface) interface ListList extends List> .由于某些原因,我无法转换 ListList至 List> .有什么方法可以做到吗?为什么它不
我想调用带有两个参数的父类(super class)的构造函数,所以我调用了 super(arguments),但是编译器说: “类 Person 中的构造函数 Person 不能应用于给定类型; 要
我是一名优秀的程序员,十分优秀!