-2

I have to pass a parameter to from client to server through spring RMI please advise how to achieve this below is the class i have editied

package com.javatpoint;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.remoting.support.RemoteInvocation;

@SuppressWarnings("serial")
public class CalculationImpl extends RemoteInvocation implements Calculation {

    public int cube(int number) {
        return number*number*number;
    }


    public CalculationImpl (MethodInvocation methodInvocation) {
        super();

        // Invoked in superclass
        this.addAttribute("awer", "test1111");
    }


    private void addAttribute(String string, String string2) {


    }
}

and the xml for the client is

<beans>

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

    </bean>

    <bean id="invocationFactory" class="com.javatpoint.CalculationImpl"/>
</beans>

now please advise how to customise the rmiservice exporter so that it should recieve the value sent fromt he client lets say as shown above in client xml the value of parameter awer is test1111 now i want to customise my rmiservice exporter so that it should recieve this value and display it

below is my rmi service exporter..

<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="registryPort" value="1099"></property>

    </bean>
SpringLearner
  • 13,738
  • 20
  • 78
  • 116

1 Answers1

0

In your 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, but you are missing a few things.

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>

you need to 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");
    }
}

finally you 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();
    }
}
QuakeCore
  • 1,886
  • 2
  • 15
  • 33
  • core ...Thanks alot can you please advise how can we send the port no as the clients are multiple and they must be running in different ports so the server should know the request is coming from whcih client so for that to know client port is eesntial Thanks inadvance – tuntun82828282 hdjscd Aug 24 '15 at 10:31
  • if you are talking about the port specified in the serviceUrl property, then no all clients must use the same port in order for them to obtain the remote object that has been set in the server's registry. – QuakeCore Aug 24 '15 at 11:07
  • Thanks for the reply no I am talking of different port that is clients are running on different port I want to fetch the port of clients pls – tuntun82828282 hdjscd Aug 24 '15 at 11:32
  • I am not sure I get what you mean by each client will run on a different port, but check [this](http://stackoverflow.com/questions/8591990/knowing-the-port-number-during-rmi) question out, it might help you! – QuakeCore Aug 24 '15 at 11:54
  • Well thanks for the reply I just want to give example let's say above client is running on different machine having different ip now I just want that client to send their respective ip,'s to the server program as rite now they are sending the text please advise how to achieve this – tuntun82828282 hdjscd Aug 24 '15 at 12:11
  • Thanks a lot but sorry can you please where you have edited. – tuntun82828282 hdjscd Aug 24 '15 at 12:37
  • see [this](http://stackoverflow.com/questions/3867197/get-the-server-port-number-from-tomcat-with-out-a-request)question if you want the webserver's port – QuakeCore Aug 24 '15 at 12:40
  • @tuntun82828282hdjscd We've had this discussion [twice already this week](http://stackoverflow.com/questions/32156010/configuring-rmi-host-so-that-it-should-know-which-client-port-is-accessing-it). The client port does not uniquely identify the client. It is therefore of no use or interest. – user207421 Aug 24 '15 at 12:45
  • Yeah agree completely but I just only need the port at this stage rest other parameters I will fetch from different way – tuntun82828282 hdjscd Aug 24 '15 at 12:46
  • @tuntun82828282hdjscd You don't need the port at all. You can't get it at the client, and it is of no conceivable use or interest at the server even if you could get it. You've just agreed. I don't know why you post here if you're just going to ignore what you're told. – user207421 Aug 24 '15 at 12:49
  • @tuntun82828282hdjscd listen to EJP, he is probably, at least 10 years ahead of us :) – QuakeCore Aug 24 '15 at 12:53
  • @EJP can you also please advise what are the rest parameters that makes client unique so that I can send those parameters also – tuntun82828282 hdjscd Aug 24 '15 at 12:54
  • @tuntun82828282hdjscd I've already given you a better answer than that, and so have others. Enough is enough. – user207421 Aug 24 '15 at 22:27