I did this to resolve my requirement:
Implement an interceptor on my service. It uses the AttributePersistenceService directly to save attributes. Implement the 'start' lifecycle method to load attributes from the AttributePersistenceService.
{code}
@Depends("jboss:service=AttributePersistenceService")
private AttributePersistenceServiceMBean attributePersistenceService;
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
Object result = context.proceed();
if(context.getMethod().getName().startsWith("set")) {
saveAttributes();
}
return result;
}
public void start() throws Exception {
loadAttributes();
}
private void saveAttributes() throws Exception {
AttributePersistenceManager manager = getAttributePersistenceManager();
if(manager != null) {
AttributeList attrs = new AttributeList();
attrs.add(new Attribute("syncInterval", syncInterval));
LOGGER.debug("saving attributes: {}", attrs);
manager.store(ATTRIBUTE_KEY, attrs);
}
}
private void loadAttributes() throws Exception {
AttributePersistenceManager manager = getAttributePersistenceManager();
if(manager != null) {
AttributeList attrs = manager.load(ATTRIBUTE_KEY);
for(Attribute attribute: attrs.asList()) {
if("syncInterval".equals(attribute.getName())) {
this.syncInterval = ((Long)attribute.getValue()).longValue();
}
}
}
}
private AttributePersistenceManager getAttributePersistenceManager() {
AttributePersistenceManager result = null;
if(attributePersistenceService != null) {
result = attributePersistenceService.apmCreate();
}
return result;
}
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
Object result = context.proceed();
if(context.getMethod().getName().startsWith("set")) {
saveAttributes();
}
return result;
}
public void start() throws Exception {
loadAttributes();
}
private void saveAttributes() throws Exception {
AttributePersistenceManager manager = getAttributePersistenceManager();
if(manager != null) {
AttributeList attrs = new AttributeList();
attrs.add(new Attribute("syncInterval", syncInterval));
LOGGER.debug("saving attributes: {}", attrs);
manager.store(ATTRIBUTE_KEY, attrs);
}
}
private void loadAttributes() throws Exception {
AttributePersistenceManager manager = getAttributePersistenceManager();
if(manager != null) {
AttributeList attrs = manager.load(ATTRIBUTE_KEY);
for(Attribute attribute: attrs.asList()) {
if("syncInterval".equals(attribute.getName())) {
this.syncInterval = ((Long)attribute.getValue()).longValue();
}
}
}
}
private AttributePersistenceManager getAttributePersistenceManager() {
AttributePersistenceManager result = null;
if(attributePersistenceService != null) {
result = attributePersistenceService.apmCreate();
}
return result;
}
{code}