Questo contenuto non è disponibile nella lingua selezionata.
8.2. A Standard MBean Example
This section develops two sample MBean services packaged together in a service archive (.sar).
ConfigServiceMBean interface declares specific methods like the start, getTimeout and stop methods to start, hold and stop the MBean correctly without using any JBoss specific classes. ConfigService class implements ConfigServiceMBean interface and consequently implements the methods used within that interface.
PlainThread class extends ServiceMBeanSupport class and implements PlainThreadMBean interface. PlainThread starts a thread and uses ConfigServiceMBean.getTimeout() to determine how long the thread should sleep.
Example 8.1. Sample MBean services
package org.jboss.example.mbean.support;
public interface ConfigServiceMBean {
int getTimeout();
void start();
void stop();
}
package org.jboss.example.mbean.support;
public class ConfigService implements ConfigServiceMBean {
int timeout;
@Override
public int getTimeout() {
return timeout;
}
@Override
public void start() {
//Create a random number between 3000 and 6000 milliseconds
timeout = (int)Math.round(Math.random() * 3000) + 3000;
System.out.println("Random timeout set to " + timeout + " seconds");
}
@Override
public void stop() {
timeout = 0;
}
}
package org.jboss.example.mbean.support;
import org.jboss.system.ServiceMBean;
public interface PlainThreadMBean extends ServiceMBean {
void setConfigService(ConfigServiceMBean configServiceMBean);
}
package org.jboss.example.mbean.support;
import org.jboss.system.ServiceMBeanSupport;
public class PlainThread extends ServiceMBeanSupport implements PlainThreadMBean {
private ConfigServiceMBean configService;
private Thread thread;
private volatile boolean done;
@Override
public void setConfigService(ConfigServiceMBean configService) {
this.configService = configService;
}
@Override
protected void startService() throws Exception {
System.out.println("Starting Plain Thread MBean");
done = false;
thread = new Thread(new Runnable() {
@Override
public void run() {
try {
while (!done) {
System.out.println("Sleeping....");
Thread.sleep(configService.getTimeout());
System.out.println("Slept!");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
thread.start();
}
@Override
protected void stopService() throws Exception {
System.out.println("Stopping Plain Thread MBean");
done = true;
}
}
package org.jboss.example.mbean.support;
public interface ConfigServiceMBean {
int getTimeout();
void start();
void stop();
}
package org.jboss.example.mbean.support;
public class ConfigService implements ConfigServiceMBean {
int timeout;
@Override
public int getTimeout() {
return timeout;
}
@Override
public void start() {
//Create a random number between 3000 and 6000 milliseconds
timeout = (int)Math.round(Math.random() * 3000) + 3000;
System.out.println("Random timeout set to " + timeout + " seconds");
}
@Override
public void stop() {
timeout = 0;
}
}
package org.jboss.example.mbean.support;
import org.jboss.system.ServiceMBean;
public interface PlainThreadMBean extends ServiceMBean {
void setConfigService(ConfigServiceMBean configServiceMBean);
}
package org.jboss.example.mbean.support;
import org.jboss.system.ServiceMBeanSupport;
public class PlainThread extends ServiceMBeanSupport implements PlainThreadMBean {
private ConfigServiceMBean configService;
private Thread thread;
private volatile boolean done;
@Override
public void setConfigService(ConfigServiceMBean configService) {
this.configService = configService;
}
@Override
protected void startService() throws Exception {
System.out.println("Starting Plain Thread MBean");
done = false;
thread = new Thread(new Runnable() {
@Override
public void run() {
try {
while (!done) {
System.out.println("Sleeping....");
Thread.sleep(configService.getTimeout());
System.out.println("Slept!");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
thread.start();
}
@Override
protected void stopService() throws Exception {
System.out.println("Stopping Plain Thread MBean");
done = true;
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
The jboss-service.xml descriptor shows how ConfigService class is injected into PlainThread class using inject tag. The inject tag establishes a dependency between PlainThreadMBean and ConfigServiceMBean and thus allows PlainThreadMBean use ConfigServiceMBean easily.
Aiutiamo gli utenti Red Hat a innovarsi e raggiungere i propri obiettivi con i nostri prodotti e servizi grazie a contenuti di cui possono fidarsi. Esplora i nostri ultimi aggiornamenti.
Rendiamo l’open source più inclusivo
Red Hat si impegna a sostituire il linguaggio problematico nel codice, nella documentazione e nelle proprietà web. Per maggiori dettagli, visita il Blog di Red Hat.
Informazioni su Red Hat
Forniamo soluzioni consolidate che rendono più semplice per le aziende lavorare su piattaforme e ambienti diversi, dal datacenter centrale all'edge della rete.