الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة في برنامج يستخدم تقنية Rmi

بدأه eihab في 6 يناير 2010 · 7 رد · 905 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ..

قمت بتطبيق مثال يستخدم تقنية ال RMI ثم قمت بتجريبه محليا على العنوان المحلي 127.0.0.1 فنفذ دون اي مشكلة

الا انه عندما اردت تطبيقه عبر الشبكة اعطى exception له علاقة بعملية marshalling و تضمن الاستثناء ايضا كلمة tcp ..

علما اني راعيت موضوع العناوين

ip

وموضوع مدير الأمن

securitymanager

#2

لا أعرف كيف يمكننا أن نفيدك

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#3
/*
your problem is probably in the random creation of the port number of the RMI
agent, this problem doesn'toccur when testing on the localhost machine,
but while connecting to different server. 
As I remember the problem occured with me only on linux machines but am not sure
now, as i solved such problem a year and half ago.

When connecting to the RMI registry through some port, there's a new random port
automatically created during the handshake process, and you wont know about the 
newly opened port unless you monitor the used ports and by which processes
by using netstat.

My solution was to implement my own RMI registry where I can manage the port
numbers being used by the RMI registery and RMI agent.
Actually I could use the same port for both (RMI registry port and RMI agent 
port) because the RMI registry port is being released after the handshake.

I would like to see the implementation of your RMI service, so I can help you 
with it, and post the whole exception you're receiving.

Hope this was helpfull

*/
1
#4

الله يطمنك ،، حالياً انا شغال في بروجيكت واستخدم فيه الـ RMI ولكني لم اجربه بعد علي شبكه محليه

حاول تفصيل المشكلة أكثر أو قم بوضع الخطأ

##############################

NullOne

شكراً جزيلا استفدت من هذه المعلومة ،، كنت أعرف انه يمكن عمل الـ RMI Registry

بنفسي ولكن كنت بقول لنفسي وايه الفايدة طالما هو موجود واحد بالفعل

ولكن الظاهر ان هناك مشاكل حلها عمل Custom RMI Registry

شكراً :)

صــفــر

#5

كيف يمكنني تخصيص RMI regustery ؟

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#6
//8000 >> Port Number
Registry reg = LocateRegistry.createRegistry(8000);


reg.rebind("name",object);

تم تعديل هذه المشاركة بواسطة cIs.Falcon في 7 يناير 2010 في 16:28

1

صــفــر

#7
/*
  This application is simple test application to explain how you can call methods
  remotely in java using JMX and RMI. If anyone have any question about it, feel free to ask

  The service is implemented in ApplicationMonitor, ApplicationMonitorMBean and Main

  ApplicationMonitorMBean: it's the MBean interface, contains the mothods that are registered
                           on the MBean server, and you can invoke remotely.
  ApplicationMonitor: Implements the ApplicationMonitorMBean.
  Main: Starts the MBean server.

  The client application that connect to the MBean server to invoke the methods is JmxClient

  */

/*
 * MBean interface, contains all the methods that can be invoked remotely
 * This interface is implemented by ApplicationMonitor
 */

public interface ApplicationMonitorMBean {

    double getPrice();
}

/**
 * Application monitor implements the ApplicationMonitorMBean
 * The methods regisert() and unregister() are used for registration and
 * unregistration of the mbean on the mbean server.
 *
 */
import java.util.Random;
import javax.management.ObjectName;

public class ApplicationMonitor implements ApplicationMonitorMBean {


    private ObjectName _mbeanName = null;
    private Random gen;
    private double price = 0.0;

    public ApplicationMonitor()
    {
        gen = new Random();
    }

    //registers the mbean on the mbean server
    public void register()
    {
            try
            {
                _mbeanName = new ObjectName("jmxtestapp:type=monitoringMBean");
                Main.MBEAN_SERVER.registerMBean(this, _mbeanName);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
    }

    // unregisters the mbean from the mbean server
    public void unregister()
    {
            if (_mbeanName != null)
            {
                try
                {
                    Main.MBEAN_SERVER.unregisterMBean(_mbeanName);
                    Main.CONNECTOR_SERVER.stop();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
    }

    // the one method that we can invoke remotely
    // it returns a random double value each time it's invoked
    public double getPrice()
    {
        if(gen != null)
        {
            return price = gen.nextDouble();
        }
        return price;
    }



}

/**
 *  The main method starts the jmx connector server by calling createJmxConnection()
 *  It creates and instance of the ApplicationMonitor and calls its register method.
 *  Then the application sleeps for 60 seconds so the client can call the mothods of
 *  ApplicationMonitorMBean within this duration.
 *  Finally the application unregisters the mbean, stops the mbean server and exit.
 *
 */

import java.io.File;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import javax.management.MBeanServer;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class Main {

    private static final String FILE_SEPARATOR = System.getProperty("file.separator");
    public static MBeanServer MBEAN_SERVER = ManagementFactory.getPlatformMBeanServer();
    public static JMXConnectorServer CONNECTOR_SERVER;
    final String registryHost = "localhost";
    final String rmiHost = "localhost";
    final int registryPort = 3333;
    final int rmiPort = registryPort;


    // creates and starts the jmx connector server
      public void createJmxConnection()
    {
        try{

         LocateRegistry.createRegistry(registryPort);

            HashMap<String, Object> env = new HashMap<String, Object>(3);

            // must be full path of the jmxremote.password file not only the name
            File dir = new File(".");
            String passwordFile = dir.getCanonicalPath() + FILE_SEPARATOR + "jmxremote.password";
            env.put("jmx.remote.x.password.file", passwordFile);

               JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://" + rmiHost + ":" + rmiPort + "/jndi/rmi://" + registryHost + ":"
                                                      + registryPort + "/jmxrmi");

                CONNECTOR_SERVER = JMXConnectorServerFactory.newJMXConnectorServer(url, env, MBEAN_SERVER);

                CONNECTOR_SERVER.start();


        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        Main test = new Main();
        ApplicationMonitor mbean = new ApplicationMonitor();

        System.out.println("Creating jmx connection");
        test.createJmxConnection();

        System.out.println("Registering to mbean server");
        mbean.register();

        System.out.println("Sleeping for 60 second");
        try{
            Thread.sleep(60000);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("Unregistering from mbean server and closing connection");
        mbean.unregister();
        System.out.println("Application exited");
    }

}

/*
 * This class implements the jmx client that connect to the mbean server
 * and call the method to be invoked and prints out the returned value.
 *
 * This application can run on different server than the other application,
 * just make sure to set the IP address and port properly.
 *
 * The method startMonitoring calls the method getPrice ApplicationMonitorMBean
 * for 10 times.
 * The main method of this class calls startMonitoring for 10 times with a
 * delay of 2 seconds between each call, before the application exits.
 */
import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;


public class jmxClient {

    //should be changed to the ip address of the machine where service is running
    private final String ipAddress = "192.168.1.243";
    private final int port = 3333;

    //creates the connection to the mbean server
     public MBeanServerConnection createMbeanServerConnection()
    {
        MBeanServerConnection connection = null;
        try
        {

            String[] credentials = new String[]
            {
                    "monitor", "password"
            };

            JMXServiceURL url;
            url = new JMXServiceURL("service:jmx:rmi://" + ipAddress + ":" + port + "/jndi/rmi://" + ipAddress + ":" + port + "/jmxrmi");
            Hashtable h = new Hashtable();
            h.put("jmx.remote.credentials", credentials);
            h.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
            JMXConnector jmxc = JMXConnectorFactory.connect(url, h);
            connection = jmxc.getMBeanServerConnection();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return connection;
     }


     public void startMonitoring()
     {
         MBeanServerConnection fembsc = createMbeanServerConnection();

        if (fembsc != null)
        {

            String name = "jmxtestapp:type=monitoringMBean";
            String result = null;
            try
            {
                ObjectName mbeanName = new ObjectName(name);
                result = String.valueOf(fembsc.getAttribute(mbeanName, "Price"));
                System.out.println("result: " + result);

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
     }
     }

     public static void main(String [] args)
     {
         jmxClient client = new jmxClient();
         for(int i=0; i<10; i++)
         {
            try {
                client.startMonitoring();
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
         }
     }

}
#8

شكرا لجميع المشاركين

انحلت مشاكل الشبكة بوضع ملف java.policy ضمن السيرفر

تم ضمنه منح كافة السماحيات عبر

grant{
permission java.security.AllPermission;
}
;

وقبل تسجيل غرض من السيرفر ضمن الريجيستري يجب كتابة التعليمة

if(System.getSecurityManager()==null) 
System.setSecurityManager(new RMISecurityManager());
try{
..
naming.rebind("//192.168.0.1/test",test);
..
}

تم تعديل هذه المشاركة بواسطة herch في 9 يناير 2010 في 21:57 — السبب: الوسم [code]

1

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…