| 1 |
package org.apache.commons.jcs3.auxiliary.lateral.socket.tcp; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import java.io.BufferedReader; |
| 23 |
import java.io.IOException; |
| 24 |
import java.io.InputStreamReader; |
| 25 |
import java.nio.charset.StandardCharsets; |
| 26 |
import java.util.Collections; |
| 27 |
import java.util.HashMap; |
| 28 |
import java.util.Map; |
| 29 |
import java.util.Set; |
| 30 |
|
| 31 |
import org.apache.commons.jcs3.auxiliary.lateral.LateralCommand; |
| 32 |
import org.apache.commons.jcs3.auxiliary.lateral.LateralElementDescriptor; |
| 33 |
import org.apache.commons.jcs3.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes; |
| 34 |
import org.apache.commons.jcs3.engine.CacheElement; |
| 35 |
import org.apache.commons.jcs3.engine.CacheInfo; |
| 36 |
import org.apache.commons.jcs3.engine.behavior.ICacheElement; |
| 37 |
import org.apache.commons.jcs3.engine.behavior.ICacheServiceNonLocal; |
| 38 |
import org.apache.commons.jcs3.engine.behavior.IElementSerializer; |
| 39 |
import org.apache.commons.jcs3.log.Log; |
| 40 |
import org.apache.commons.jcs3.log.LogManager; |
| 41 |
import org.apache.commons.jcs3.utils.serialization.StandardSerializer; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class LateralTCPService<K, V> |
| 48 |
implements ICacheServiceNonLocal<K, V> |
| 49 |
{ |
| 50 |
|
| 51 |
private static final Log log = LogManager.getLog( LateralTCPService.class ); |
| 52 |
|
| 53 |
|
| 54 |
private final boolean allowPut; |
| 55 |
private final boolean allowGet; |
| 56 |
private final boolean issueRemoveOnPut; |
| 57 |
|
| 58 |
|
| 59 |
private final LateralTCPSender sender; |
| 60 |
|
| 61 |
|
| 62 |
private long listenerId = CacheInfo.listenerId; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@Deprecated |
| 73 |
public LateralTCPService( final ITCPLateralCacheAttributes lca ) |
| 74 |
throws IOException |
| 75 |
{ |
| 76 |
this(lca, new StandardSerializer()); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public LateralTCPService( final ITCPLateralCacheAttributes lca, final IElementSerializer serializer ) |
| 88 |
throws IOException |
| 89 |
{ |
| 90 |
this.allowGet = lca.isAllowGet(); |
| 91 |
this.allowPut = lca.isAllowPut(); |
| 92 |
this.issueRemoveOnPut = lca.isIssueRemoveOnPut(); |
| 93 |
|
| 94 |
try |
| 95 |
{ |
| 96 |
sender = new LateralTCPSender( lca, serializer ); |
| 97 |
|
| 98 |
log.debug( "Created sender to [{0}]", lca::getTcpServer); |
| 99 |
} |
| 100 |
catch ( final IOException e ) |
| 101 |
{ |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
log.error( "Could not create sender to [{0}] -- {1}", lca::getTcpServer, e::getMessage); |
| 106 |
throw e; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
@Override |
| 115 |
public void update( final ICacheElement<K, V> item ) |
| 116 |
throws IOException |
| 117 |
{ |
| 118 |
update( item, getListenerId() ); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
@Override |
| 130 |
public void update( final ICacheElement<K, V> item, final long requesterId ) |
| 131 |
throws IOException |
| 132 |
{ |
| 133 |
|
| 134 |
if ( !this.allowPut && |
| 135 |
|
| 136 |
!this.issueRemoveOnPut ) |
| 137 |
{ |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
if ( !this.issueRemoveOnPut ) |
| 143 |
{ |
| 144 |
final LateralElementDescriptor<K, V> led = |
| 145 |
new LateralElementDescriptor<>(item, LateralCommand.UPDATE, requesterId); |
| 146 |
sender.send( led ); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
else |
| 151 |
{ |
| 152 |
log.debug( "Issuing a remove for a put" ); |
| 153 |
|
| 154 |
|
| 155 |
final CacheElement<K, V> ce = new CacheElement<>( item.getCacheName(), item.getKey(), null ); |
| 156 |
final LateralElementDescriptor<K, V> led = |
| 157 |
new LateralElementDescriptor<>(ce, LateralCommand.REMOVE, requesterId); |
| 158 |
led.valHashCode = item.getVal().hashCode(); |
| 159 |
sender.send( led ); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
@Override |
| 169 |
public void remove( final String cacheName, final K key ) |
| 170 |
throws IOException |
| 171 |
{ |
| 172 |
remove( cacheName, key, getListenerId() ); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@Override |
| 181 |
public void remove( final String cacheName, final K key, final long requesterId ) |
| 182 |
throws IOException |
| 183 |
{ |
| 184 |
final CacheElement<K, V> ce = new CacheElement<>( cacheName, key, null ); |
| 185 |
final LateralElementDescriptor<K, V> led = |
| 186 |
new LateralElementDescriptor<>(ce, LateralCommand.REMOVE, requesterId); |
| 187 |
sender.send( led ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
@Override |
| 196 |
public void release() |
| 197 |
throws IOException |
| 198 |
{ |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
@Override |
| 209 |
public void dispose( final String cacheName ) |
| 210 |
throws IOException |
| 211 |
{ |
| 212 |
sender.dispose(); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
@Override |
| 222 |
public ICacheElement<K, V> get( final String cacheName, final K key ) |
| 223 |
throws IOException |
| 224 |
{ |
| 225 |
return get( cacheName, key, getListenerId() ); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
@Override |
| 238 |
public ICacheElement<K, V> get( final String cacheName, final K key, final long requesterId ) |
| 239 |
throws IOException |
| 240 |
{ |
| 241 |
|
| 242 |
if ( this.allowGet ) |
| 243 |
{ |
| 244 |
final CacheElement<K, V> ce = new CacheElement<>( cacheName, key, null ); |
| 245 |
final LateralElementDescriptor<K, V> led = |
| 246 |
new LateralElementDescriptor<>(ce, LateralCommand.GET); |
| 247 |
|
| 248 |
@SuppressWarnings("unchecked") |
| 249 |
final |
| 250 |
ICacheElement<K, V> response = (ICacheElement<K, V>)sender.sendAndReceive( led ); |
| 251 |
return response; |
| 252 |
} |
| 253 |
|
| 254 |
return null; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
@Override |
| 267 |
public Map<K, ICacheElement<K, V>> getMatching( final String cacheName, final String pattern ) |
| 268 |
throws IOException |
| 269 |
{ |
| 270 |
return getMatching( cacheName, pattern, getListenerId() ); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
@Override |
| 284 |
@SuppressWarnings("unchecked") |
| 285 |
public Map<K, ICacheElement<K, V>> getMatching( final String cacheName, final String pattern, final long requesterId ) |
| 286 |
throws IOException |
| 287 |
{ |
| 288 |
|
| 289 |
if ( !this.allowGet ) { |
| 290 |
|
| 291 |
return null; |
| 292 |
} |
| 293 |
final CacheElement<String, String> ce = new CacheElement<>( cacheName, pattern, null ); |
| 294 |
final LateralElementDescriptor<String, String> led = |
| 295 |
new LateralElementDescriptor<>(ce, LateralCommand.GET_MATCHING); |
| 296 |
|
| 297 |
|
| 298 |
final Object response = sender.sendAndReceive( led ); |
| 299 |
if ( response != null ) |
| 300 |
{ |
| 301 |
return (Map<K, ICacheElement<K, V>>) response; |
| 302 |
} |
| 303 |
return Collections.emptyMap(); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
@Override |
| 316 |
public Map<K, ICacheElement<K, V>> getMultiple( final String cacheName, final Set<K> keys ) |
| 317 |
throws IOException |
| 318 |
{ |
| 319 |
return getMultiple( cacheName, keys, getListenerId() ); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
@Override |
| 335 |
public Map<K, ICacheElement<K, V>> getMultiple( final String cacheName, final Set<K> keys, final long requesterId ) |
| 336 |
throws IOException |
| 337 |
{ |
| 338 |
final Map<K, ICacheElement<K, V>> elements = new HashMap<>(); |
| 339 |
|
| 340 |
if ( keys != null && !keys.isEmpty() ) |
| 341 |
{ |
| 342 |
for (final K key : keys) |
| 343 |
{ |
| 344 |
final ICacheElement<K, V> element = get( cacheName, key, requesterId ); |
| 345 |
|
| 346 |
if ( element != null ) |
| 347 |
{ |
| 348 |
elements.put( key, element ); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
return elements; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
@Override |
| 362 |
@SuppressWarnings("unchecked") |
| 363 |
public Set<K> getKeySet(final String cacheName) throws IOException |
| 364 |
{ |
| 365 |
final CacheElement<String, String> ce = new CacheElement<>(cacheName, null, null); |
| 366 |
final LateralElementDescriptor<String, String> led = |
| 367 |
new LateralElementDescriptor<>(ce, LateralCommand.GET_KEYSET); |
| 368 |
|
| 369 |
final Object response = sender.sendAndReceive(led); |
| 370 |
if (response != null) |
| 371 |
{ |
| 372 |
return (Set<K>) response; |
| 373 |
} |
| 374 |
|
| 375 |
return null; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
@Override |
| 383 |
public void removeAll( final String cacheName ) |
| 384 |
throws IOException |
| 385 |
{ |
| 386 |
removeAll( cacheName, getListenerId() ); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
@Override |
| 395 |
public void removeAll( final String cacheName, final long requesterId ) |
| 396 |
throws IOException |
| 397 |
{ |
| 398 |
final CacheElement<String, String> ce = new CacheElement<>( cacheName, "ALL", null ); |
| 399 |
final LateralElementDescriptor<String, String> led = |
| 400 |
new LateralElementDescriptor<>(ce, LateralCommand.REMOVEALL, requesterId); |
| 401 |
sender.send( led ); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
@Deprecated |
| 411 |
public static void main( final String args[] ) |
| 412 |
{ |
| 413 |
try |
| 414 |
{ |
| 415 |
final LateralTCPSender sender = new LateralTCPSender( new TCPLateralCacheAttributes() ); |
| 416 |
|
| 417 |
|
| 418 |
boolean notDone = true; |
| 419 |
String message = null; |
| 420 |
|
| 421 |
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); |
| 422 |
|
| 423 |
while ( notDone ) |
| 424 |
{ |
| 425 |
System.out.println( "enter message:" ); |
| 426 |
message = br.readLine(); |
| 427 |
|
| 428 |
if (message == null) |
| 429 |
{ |
| 430 |
notDone = false; |
| 431 |
continue; |
| 432 |
} |
| 433 |
|
| 434 |
final CacheElement<String, String> ce = new CacheElement<>( "test", "test", message ); |
| 435 |
final LateralElementDescriptor<String, String> led = new LateralElementDescriptor<>( ce ); |
| 436 |
sender.send( led ); |
| 437 |
} |
| 438 |
} |
| 439 |
catch ( final IOException e ) |
| 440 |
{ |
| 441 |
System.out.println( e.toString() ); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
protected void setListenerId( final long listernId ) |
| 449 |
{ |
| 450 |
this.listenerId = listernId; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
protected long getListenerId() |
| 457 |
{ |
| 458 |
return listenerId; |
| 459 |
} |
| 460 |
} |