1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.rribbit.dispatching;
17
18 import java.rmi.registry.LocateRegistry;
19 import java.rmi.registry.Registry;
20 import java.rmi.server.RMIClientSocketFactory;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.Random;
25
26 import javax.rmi.ssl.SslRMIClientSocketFactory;
27
28 import org.rribbit.Request;
29 import org.rribbit.Response;
30 import org.rribbit.processing.RmiRequestProcessor;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40
41
42
43 public class RmiRequestDispatcher implements RequestDispatcher {
44
45 private static final Logger log = LoggerFactory.getLogger(RmiRequestDispatcher.class);
46
47 protected int retryAttempts = 10;
48 protected int portnumber;
49 protected String[] hosts;
50 protected String truststoreLocation;
51
52
53
54
55
56
57
58
59
60
61 public RmiRequestDispatcher(int portnumber, String... hosts) {
62
63 this.portnumber = portnumber;
64 this.hosts = hosts;
65 }
66
67
68
69
70
71
72
73
74
75
76
77 public RmiRequestDispatcher(String truststoreLocation, int portnumber, String... hosts) {
78
79 this.truststoreLocation = truststoreLocation;
80 this.portnumber = portnumber;
81 this.hosts = hosts;
82
83 System.setProperty("javax.net.ssl.trustStore", truststoreLocation);
84 }
85
86 @Override
87 public <T> Response<T> dispatchRequest(Request request) {
88
89 List<String> hostsAsList = new ArrayList<>(Arrays.asList(hosts));
90
91 log.info("Connecting to an RmiRequestProcessorImpl");
92 for(int i=0; i<retryAttempts; i++) {
93 log.info("Attempt: {} (Max Attempts: {})", i+1, retryAttempts);
94
95
96 if(hostsAsList.isEmpty()) {
97 log.info("All hosts have been tried. Re-loading...");
98 hostsAsList = new ArrayList<>(Arrays.asList(hosts));
99 }
100
101
102 int number = new Random().nextInt(hostsAsList.size());
103 String host = hostsAsList.remove(number);
104
105
106 try {
107 log.info("Connecting to server: '{}:{}'", host, portnumber);
108 Registry registry;
109 if(truststoreLocation == null) {
110 registry = LocateRegistry.getRegistry(host, portnumber);
111 } else {
112 RMIClientSocketFactory csf = new SslRMIClientSocketFactory();
113 registry = LocateRegistry.getRegistry(host, portnumber, csf);
114 }
115
116 RmiRequestProcessor rmiRequestProcessor = (RmiRequestProcessor) (registry.lookup(RmiRequestProcessor.REGISTRY_KEY));
117 Response<T> response = rmiRequestProcessor.processRequestViaRMI(request);
118 log.info("Returning Response");
119 return response;
120 } catch(Exception e) {
121 log.error("Connection failed, trying again...", e);
122 }
123 }
124 log.error("No connection to an RmiRequestProcessorImpl could be made");
125 throw new RuntimeException("No connection to an RmiRequestProcessorImpl could be made");
126 }
127
128
129
130
131 public int getRetryAttempts() {
132 return retryAttempts;
133 }
134
135
136
137
138
139
140 public void setRetryAttempts(int retryAttempts) {
141 this.retryAttempts = retryAttempts;
142 }
143 }