0

I have the below program of Spring RMI in which the client send its port no which i s received at the Server end and is being displayed , I am using spring rmi to achieve that now i want is there any way since i want to remove the hard coding that is client would notsent it hostname now simply client would call the servre RMI server and RMI server should be smart enough to judge that the client host name , please advise f in the below program i stop passing the hostname from client side then how would be the server would be smart enough to judge the client hostname itself

calculation interface there should be a method that takes a String as a parameter.

public interface Calculation {  
void print(String text );  
void registerIpAddress(String ip);
} 

public class CalculationImpl implements Calculation {

@Override
void print(String text) {
    System.out.println(text);
}
    @Override
    void registerIpAddress(String ip){
            List.add(ip);//or whatever you want
}

your xml files for both client and host are right ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="calculationBean" class="com.javatpoint.CalculationImpl"></bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="service" ref="calculationBean"></property>
    <property name="serviceInterface" value="com.javatpoint.Calculation"></property>
    <property name="serviceName" value="CalculationService"></property>
    <property name="replaceExistingBinding" value="true"></property>
    <property name="registryPort" value="1099"></property>
</bean>
</beans>

client-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="calculationBean" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/CalculationService"></property>
<property name="serviceInterface" value="com.javatpoint.Calculation"></property>
</bean>
</beans>

create a new instance of applicaionContext in order for this to work

class server {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
}

need to run your client

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
    Calculation calculation = (Calculation) context.getBean("calculationBean");
    calculation.print("Copied&Pasted!");

    try {
        calculation.registerIpAddress(InetAddress.getLocalHost().getHostAddress());
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

upon googling i can see the method

public static String getClientHost()
                            throws ServerNotActiveException

now for this i need to extend remote server class can't i achieve same thing in spring rmi itself

  • If you can't find such a method in the Spring RMI API, there isn't one. If you haven't looked yet, start now. You were asked several questions ago what the ultimate objective is here. You still haven't answered. – user207421 Aug 25 '15 at 06:52
  • Well there must be someway by extending from remote service exporter class or some listener where we can code that whenever the client call the rmi server at then stage itself n listener is fired which I'll fetch the client port itself – tuntun82828282 hdjscd Aug 25 '15 at 10:17
  • Rite now as you can see that it is the client which is passing it's host name but I want to make client lite that is it will not send the port no it is the rmi server which should be smart enough to fetch the client port no itself – tuntun82828282 hdjscd Aug 25 '15 at 10:20
  • If there is such a method, a search of the API will find it. Merely assuming it exists even though you can't find it is asinine. I don't want to hear another word about the 'client port no', which I have refuted *ad nauseam*, but I'm still waiting to hear what problem you're trying to solve. NB don't try to 'bump' your posts here. – user207421 Aug 25 '15 at 10:25
  • Can't it be possible to use get client host method of remote server in spring api itself – tuntun82828282 hdjscd Aug 25 '15 at 11:15
  • I answered that [yesterday](http://stackoverflow.com/a/32170381/207421). You seem to be just wasting time, and expecting people to repeat themselves like parrots. – user207421 Aug 25 '15 at 11:28
  • @EJP I agree again with you now as you have said that there is method getClientHost() API in RemoteServer , so please advise how can i add this method in my above program http://stackoverflow.com/questions/32156010/configuring-rmi-host-so-that-it-should-know-which-client-port-is-accessing-it/32170381#32170381 – tuntun82828282 hdjscd Aug 25 '15 at 15:12
  • Spring remoting does not provide equivalent method of RemoteServer's getClientHost(). If you need IP, then proceed with InetAddress.getLocalHost().getHostAddress(). If you don't want to use Spring RMI solution, move to normal RemoteServer mechanism and have access to getClientHost() – Ravindra babu Aug 25 '15 at 17:40
  • Thanks sunrise so in the solution above shall I remove the method of register ip as I do not wish to client send it's ip infact to further improve it I shall designed a class at server side which will extend the remote service exporter of spring and create a method getclientip() in which I will put this inet address code I will take the Regency of it and in the xml of server side I will provide the name of my class which has extend the remote service exporter class please advise is this solution is good – tuntun82828282 hdjscd Aug 25 '15 at 18:02

0 Answers0