KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > mbeans > rmi > MyRemoteServiceObject


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.examples.mbeans.rmi;
10
11 import java.rmi.RemoteException JavaDoc;
12 import java.rmi.server.RemoteServer JavaDoc;
13 import java.rmi.server.UnicastRemoteObject JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15
16 /**
17  * The service implementation. <br />
18  * It exposes two interfaces: the RMI Remote interface, invocable from remote clients -
19  * represented by the {@link MyRemoteService} interface, and
20  * the management interface - represented by the {@link MyRemoteServiceObjectMBean} interface,
21  * invocable from management applications that wants to manage the features of this
22  * service.
23  *
24  * @version $Revision: 1.1 $
25  */

26 public class MyRemoteServiceObject extends RemoteServer JavaDoc implements MyRemoteService, MyRemoteServiceObjectMBean
27 {
28    private boolean m_running;
29
30    public MyRemoteServiceObject() throws RemoteException JavaDoc
31    {
32    }
33
34    public void sayHello(String JavaDoc name) throws RemoteException JavaDoc
35    {
36       System.out.println("Hello, " + name);
37    }
38
39    public void start() throws Exception JavaDoc
40    {
41       if (!m_running)
42       {
43          UnicastRemoteObject.exportObject(this);
44          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
45          ctx.rebind(JNDI_NAME, this);
46          m_running = true;
47          System.out.println("My remote service started successfully");
48       }
49    }
50
51    public void stop() throws Exception JavaDoc
52    {
53       if (m_running)
54       {
55          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
56          ctx.unbind(JNDI_NAME);
57          UnicastRemoteObject.unexportObject(this, false);
58          m_running = false;
59          System.out.println("My remote service stopped successfully");
60       }
61    }
62
63    public boolean isRunning()
64    {
65       return m_running;
66    }
67 }
68
Popular Tags