I am trying to add arguments in RMI method. When I add e.g. String everything works fine. But I am not sure if I can pass an object I created. I am new to RMI so my code is very simple:
HelloIF
public interface HelloIF extends Remote {
String greeting(Context c) throws RemoteException;
}
Hello
public class Hello extends UnicastRemoteObject implements HelloIF {
public Hello() throws RemoteException {
}
public String greeting(Context c) throws RemoteException {
addToContext(c);
report(c);
return "greeting";
}
void addToContext(Context c) {
c.addID(Thread.currentThread().getId());
}
void report(Context c) {
System.out.println("Hello.greeting() thread : "
+ Thread.currentThread().getName() + " "
+ Thread.currentThread().getId());
System.out.println("Hello.greeting() context : "
+ c.getDistributedThreadName() + " " + c.getRequestType());
}
}
RMIServer
public class RMIServer {
public static void main(String[] args) throws RemoteException, MalformedURLException {
LocateRegistry.createRegistry(1099);
HelloIF hello = new Hello();
Naming.rebind("server.Hello", hello);
System.out.println("server.RMI Server is ready.");
System.out.println("RMIServer.main() thread : " + Thread.currentThread().getName()
+ " " + Thread.currentThread().getId());
}
}
RMIClient
public class RMIClient {
public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException {
Context context = new Context("request1", Thread.currentThread().getName()+System.currentTimeMillis());
Registry registry = LocateRegistry.getRegistry("localhost");
HelloIF hello = (HelloIF) registry.lookup("server.Hello");
System.out.println(hello.greeting(context));
System.out.println("RMIClient.mian() thread : " + Thread.currentThread().getName()
+ " " + Thread.currentThread().getId());
}
}
and finally my class Context
public class Context
{
private String requestType;
private String distributedThreadName;
private List<Long> IDList;
(...) getters/setters
}
What should I do to make passing Context possible?
Your object should implement Serializable. As I can see this would be one problem. It is needed because the communication between both parts is done using serialization, so each object that needs to be sent to the other part, needs to be an instance of class implementing Serializable.
public class Context implements Serializable
{
private String requestType;
private String distributedThreadName;
private List<Long> IDList;
(...) getters/setters
}
and please add a serialVersionUID as a good practice. Something like:
private static final long serialVersionUID = 20120731125400L;
Related
My aim is to make a simple chat program. I'm new at RMI. What I've got so far is that the server works. I start it. Then I start the client, it transfers the strings to the server through RMI. But then it doesn't appear on the GUI I made. That's where my problem lies.
My project structure
My StartClient class. I created a chatClient, and put the chatServer stub as parameter.
public StartClient() throws RemoteException, NotBoundException, MalformedURLException {
chatServer = (ChatServer) Naming.lookup("rmi://localhost:1099/chatServer");
}
private void run() throws RemoteException, MalformedURLException, NotBoundException {
ChatClientImpl chatClient1 = new ChatClientImpl(chatServer, "ikke");
new ChatFrame(chatClient1);
ChatClientImpl chatClient2 = new ChatClientImpl(chatServer, "bla");
new ChatFrame(chatClient2);
}
public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
StartClient start = new StartClient();
start.run();
}
In the ChatClientImpl constructor I use the remote method register.
public ChatClientImpl(ChatServer chatServer, String name) throws MalformedURLException, NotBoundException, RemoteException {
this.chatServer = chatServer;
this.name = name;
chatServer.register(this);
}
Now we're in the ChatServerImpl class, in the REGISTER method. I add the client to an ArrayList of clients. Then I use the method SENT to display the text. It calls the RECEIVE method that each client object has.
public class ChatServerImpl extends UnicastRemoteObject implements ChatServer {
private List<ChatClient> clients;
public ChatServerImpl() throws RemoteException {
this.clients = new ArrayList<ChatClient>();
}
public void register(ChatClientImpl client) throws RemoteException {
clients.add(client);
send("server", client.getName() + " has entered the room");
}
public void unregister(ChatClientImpl client) throws RemoteException {
clients.remove(client);
send("server", client.getName() + " has left the room");
}
public void send(String name, String message) throws RemoteException {
for(ChatClient client : clients) {
client.receive(name + ": " + message);
}
}
}
This is where things go wrong. The textReceiver is ALWAYS null. (textReceiver is attribute/field of the client object.)
public void receive(String message) {
if (textReceiver == null) return;
textReceiver.receive(message);
}
The ArrayList of clients are server-side and all the clients in there all have their textReceivers set on null. If you look back at StartClient there's an important line. The new ChatFrame(chatClient). In the ChatFrame's constructor is where I set the textReceiver.
public ChatFrame(ChatClientImpl chatClient) {
this.chatClient = chatClient;
chatClient.setTextReceiver(this);
String name = chatClient.getName();
setTitle("Chat: " + name);
createComponents(name);
layoutComponents();
addListeners();
setSize(300, 300);
setVisible(true);
}
This project works when I don't use RMI and they're in one package but once I separate them into client-server this problem arose. How do I communicate between them? Server-side I have an (irrelevant?) list of ChatClients that don't influence anything even though the text arrives.
Do I use RMI for every separate ChatClient and make the ChatServer connect with it and send the text like that? Seems very complicated to me. How do I go about this?
EDIT:
ChatClientImpl class
public class ChatClientImpl implements ChatClient, Serializable {
private ChatServer chatServer;
private TextReceiver textReceiver;
private String name;
public ChatClientImpl(ChatServer chatServer, String name) throws MalformedURLException, NotBoundException, RemoteException {
this.chatServer = chatServer;
this.name = name;
chatServer.register(this);
}
public String getName() {
return name;
}
public void send(String message) throws RemoteException {
chatServer.send(name, message);
}
public void receive(String message) {
if (textReceiver == null) return;
textReceiver.receive(message);
}
public void setTextReceiver(TextReceiver textReceiver) {
this.textReceiver = textReceiver;
}
public void unregister() throws RemoteException {
chatServer.unregister(this);
}
}
Your ChatClientImpl class isn't an exported remote object, so it will be serialized to the server, and execute there. And because register() happens during construction, it will be serialized before the setReceiverTextReceiver() method is called. So, the corresponding field will be null. At the server. This is not what you want and it is also not where you want it.
So, make it extend UnicastRemoteObject and implement your ChatClient (presumed) remote interface. If you have problems with doing that, solve them. Don't just mess around with things arbitrarily. And it should not implement Serializable.
NB The signature of register() should be register(ChatClient client). Nothing to do with the ChatClientImpl class. Ditto for unregister().
I have a class RabbitQueue which basically acts like a queue and implements my Pollable interface.
I also have a class SaveToDatabaseStrategy which implements my DataProcessingStrategy interface. This is designed following the strategy-pattern.
Now, my class InputHandler which implements my interface InputListener, contains an instance of the Pollable interface and one of the DataProcessingStrategy interface.
However, I don't want to set the Generic type (String) when I declare these two fields, since the Generic type depends on the implementation of this interface which is given later on.
How would you design this?
public interface Pollable<T> {
T poll();
}
public class RabbitQueue implements Pollable<String> {
// code..
}
public interface DataProcessingStrategy<T> {
void processData(T t);
}
public class SaveToDatabaseStrategy<T> implements DataProcessingStrategy<T> {
private Repository<T, ?> repo;
public SaveToDatabaseStrategy(Repository<T, ?> repo) {
this.repo = repo;
}
#Override
public void processData(T data) {
repo.create(data);
System.out.printf("Received data of type %s: %s\n", data.getClass().getSimpleName(), data);
}
}
public interface InputListener<T> {
void inputReceived();
void inputReceived(T t);
}
public class InputHandler implements InputListener<String> {
private Pollable<String> queue;
private DataProcessingStrategy<String> strategy;
public InputHandler(String host, String queueName) throws IOException, TimeoutException {
queue = new RabbitQueue(host, queueName, this);
}
public void setStrategy(DataProcessingStrategy strategy) {
this.strategy = strategy;
}
#Override
public void inputReceived() {
System.out.println("Input received!");
strategy.processData(queue.poll());
}
#Override
public void inputReceived(String s) {
System.out.println("Input received: " + s + "!");
System.out.println("> " + queue.poll());
}
}
You could add a type parameter to the InputHandler class.
public class InputHandler<T> implements InputListener<T> {
private Pollable<T> queue;
private DataProcessingStrategy<T> strategy;
public InputHandler(String host, String queueName) throws IOException, TimeoutException {
queue = new RabbitQueue(host, queueName, this);
}
public void setStrategy(DataProcessingStrategy strategy) {
this.strategy = strategy;
}
#Override
public void inputReceived() {
System.out.println("Input received!");
strategy.processData(queue.poll());
}
#Override
public void inputReceived(String s) {
System.out.println("Input received: " + s + "!");
System.out.println("> " + queue.poll().toString());
}
}
Then create a new object like
new InputHandler<String>(host, queueName)
I am trying to send messages from anywhere in my application using STOMP and websockets. However, I am having trouble because I cannot make the "greet" method static because of the "this.template" inside the method. Then I cannot make a call to the method. How can I fix this issue?
Here is my Controller class:
#Controller
public class HelloController {
#Autowired
private SimpMessagingTemplate template;
#Autowired
public HelloController(SimpMessagingTemplate template) {
this.template = template;
}
public HelloController() {
}
public static void replier(String reply) {
greet(reply);
}
#RequestMapping(value="/hello", method=RequestMethod.POST)
public void greet(String greeting) {
Greeting text = new Greeting("Goodbye, " + greeting + "!");
this.template.convertAndSend("/topic/greetings", text);
}
#RequestMapping(value="/", method=RequestMethod.GET)
public String index() {
return "index";
}
#MessageMapping("/hello")
#SendTo("/queue/greetings")
public static Greeting greeting(HelloMessage message) throws Exception {
System.out.println("Sending message...");
beginRoute(message.getName());
return new Greeting("Hello, " + message.getName() + "!");
}
#SendTo("/queue/informer")
public static Greeting beginRoute(String message) {
Application.startBody(message);
//System.out.println("Returning from second message!");
return new Greeting("So long, " + message + "!");
}
The call of greet(reply) in the replier method is invalid because I cannot make a static call to a non-static method. How can I call greet and get the message sent?
I don't understand why you think greeting needs to be static.
I found this on the websocket documentation:
#Controller
public class GreetingController {
#MessageMapping("/greeting") {
public String handle(String greeting) {
return "[" + getTimestamp() + ": " + greeting;
}
}
Try making greeting not static. If you have problems with a nonstatic method please let us know what they are.
So I have this code:
public class RemoteImpl extends UnicastRemoteObject implements TestRemote {
private static final long serialVersionUID = 1L;
private static int counter = 0;
private int localizedCounter = 0;
protected RemoteImpl() throws RemoteException {
super();
}
#Override
public int getMeGlobalCounter() throws RemoteException {
counter++;
return counter;
}
#Override
public int getMeLocalizedCounter() throws RemoteException {
localizedCounter++;
return localizedCounter;
}
}
And with my Client I am trying:
public class TestClient {
public static void main(String[] args) throws Exception {
Registry registry = LocateRegistry.getRegistry("localhost", Constant.RMI_PORT);
TestRemote remote = (TestRemote) registry.lookup(Constant.RMI_ID);
System.out.println("Global counter:" + remote.getMeGlobalCounter());
System.out.println("Localized counter:" + remote.getMeLocalizedCounter());
}
}
After running this code for the 2 times I am expecting to see:
Global counter:3
Localized counter:1
however I see that
Localized counter:3
So why is the localized counter not reset everytime I invoke this method? Am I not getting a new object everytime?
Am I not getting a new object every time?
No you aren't. You're getting the same instance that was bound into the Registry. RMI doesn't just create remote objects willy-nilly.
In a simple RMI program I managed to pass Context between two Threads. Now I need to move setting/reporting from Context to AspectJ class.
My problem is: How to move Context if I need to use it as an argument in greeting(Context)
HelloIF
public interface HelloIF extends Remote {
String greeting(Context c) throws RemoteException;
}
Hello
public class Hello extends UnicastRemoteObject implements HelloIF {
public Hello() throws RemoteException {
}
public String greeting(Context c) throws RemoteException {
c.report();
return "greeting";
}
}
RMIServer
public class RMIServer {
public static void main(String[] args) throws RemoteException, MalformedURLException {
LocateRegistry.createRegistry(1099);
HelloIF hello = new Hello();
Naming.rebind("server.Hello", hello);
System.out.println("server.RMI Server is ready.");
}
}
RMIClient
public class RMIClient {
public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException {
Context context = new Context("request1", Thread.currentThread().getName()+System.currentTimeMillis());
Registry registry = LocateRegistry.getRegistry("localhost");
HelloIF hello = (HelloIF) registry.lookup("server.Hello");
System.out.println(hello.greeting(context));
context.report();
}
}
Context
public class Context implements Serializable
{
private String requestType;
private String distributedThreadName;
public Context(String requestType, String distributedThreadName)
{
this.requestType = requestType;
this.distributedThreadName = distributedThreadName;
}
(...)
public void report() {
System.out.println("thread : "
+ Thread.currentThread().getName() + " "
+ Thread.currentThread().getId());
System.out.println("context : "
+ this.getDistributedThreadName() + " " + this.getRequestType());
}
}
and finally an empty AspectJ class
#Aspect
public class ReportingAspect {
#Before("call(void main(..))")
public void beforeReportClient(JoinPoint joinPoint) throws Throwable {
}
#After("call(void main(..))")
public void afterReportClient(JoinPoint joinPoint) throws Throwable {
}
#Before("call(String greeting(..))")
public void beforeReportGreeting(JoinPoint joinPoint) throws Throwable {
}
#After("call(String greeting(..))")
public void afterReportGreeting(JoinPoint joinPoint) throws Throwable {
}
}
How can I move from Hello and RMIClient Context() constructor and c/context.report()s to ReportingAspect?
You can pass the arguments to a function, and the underlying object, to Advice, thus:
#Before("execution(* greeting(..)) && target(target) && " +
"args(context)")
public void beforeReportGreeting(HelloIF target, Context context) {
context.doSomething();
target.doSomething();
}
Study the AspectJ annotation documentation for the full details. It can be done for all the advice types.
Edit Reading the question in more details, it sounds as if you want to make the Context object something constructed and controlled by the aspect, while still passing it as an argument to Hello.greeting().
That's not something that makes sense. Your underlying system ought to work OK without any AOP going on. So if the Context object is part of that underlying domain, then it's not a good idea for the Aspect to be in charge of its construction and management.
If the Context is only relevant to the Aspect, then you would remove all reference to the context from the domain classes (so greeting() would take no parameters) and build the Context object(s) in the Aspect.