Java Code Examples for io.nats.client.Connection#request()
The following examples show how to use
io.nats.client.Connection#request() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testRequireCleanupOnTimeout() throws IOException, ExecutionException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false)) { Options options = new Options.Builder().server(ts.getURI()).requestCleanupInterval(Duration.ofHours(1)).build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Future<Message> incoming = nc.request("subject", null); Message msg = null; try { msg = incoming.get(100, TimeUnit.MILLISECONDS); assertFalse(true); } catch(TimeoutException e) { assertTrue(true); } assertNull(msg); assertEquals(1, ((NatsStatistics)nc.getStatistics()).getOutstandingRequests()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
Example 2
Source File: RequestTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testRequireCleanupOnCancel() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false)) { Options options = new Options.Builder().server(ts.getURI()).requestCleanupInterval(Duration.ofHours(1)).build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Future<Message> incoming = nc.request("subject", null); incoming.cancel(true); assertEquals(1, ((NatsStatistics)nc.getStatistics()).getOutstandingRequests()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
Example 3
Source File: NatsReq.java From nats.java with Apache License 2.0 | 5 votes |
public static void main(String args[]) { String subject; String message; String server; if (args.length == 3) { server = args[0]; subject = args[1]; message = args[2]; } else if (args.length == 2) { server = Options.DEFAULT_URL; subject = args[0]; message = args[1]; } else { usage(); return; } try { Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, false)); Future<Message> replyFuture = nc.request(subject, message.getBytes(StandardCharsets.UTF_8)); Message reply = replyFuture.get(5, TimeUnit.SECONDS); System.out.println(); System.out.printf("Received reply \"%s\" on subject \"%s\"\n", new String(reply.getData(), StandardCharsets.UTF_8), reply.getSubject()); System.out.println(); nc.close(); } catch (Exception exp) { exp.printStackTrace(); } }
Example 4
Source File: StanExit.java From nats.java with Apache License 2.0 | 5 votes |
public static void main(String args[]) { String server = "help"; if (args.length == 1) { server = args[0]; } else if (args.length == 0) { server = Options.DEFAULT_URL; } if (server.equals("help")) { usage(); return; } try { Options options = new Options.Builder().server(server).noReconnect().build(); Connection nc = Nats.connect(options); Future<Message> replyFuture = nc.request("stan.exit", null); Message reply = replyFuture.get(); System.out.printf("I asked stan to exit without confirmation, he replied \"%s\"\n", new String(reply.getData(), StandardCharsets.UTF_8)); replyFuture = nc.request("stan.exit", "confirm".getBytes(StandardCharsets.UTF_8)); reply = replyFuture.get(); System.out.printf("I asked stan to exit with confirmation, he replied \"%s\"\n", new String(reply.getData(), StandardCharsets.UTF_8)); nc.close(); } catch (Exception exp) { exp.printStackTrace(); } }
Example 5
Source File: StanRandom.java From nats.java with Apache License 2.0 | 5 votes |
public static void main(String args[]) { String server = "help"; if (args.length == 1) { server = args[0]; } else if (args.length == 0) { server = Options.DEFAULT_URL; } if (server.equals("help")) { usage(); return; } try { Options options = new Options.Builder().server(server).noReconnect().build(); Connection nc = Nats.connect(options); Future<Message> replyFuture = nc.request("stan.random", null); Message reply = replyFuture.get(); System.out.printf("The next stan-random number is %s\n", new String(reply.getData(), StandardCharsets.UTF_8)); nc.close(); } catch (Exception exp) { exp.printStackTrace(); } }
Example 6
Source File: StanTime.java From nats.java with Apache License 2.0 | 5 votes |
public static void main(String args[]) { String server = "help"; if (args.length == 1) { server = args[0]; } else if (args.length == 0) { server = Options.DEFAULT_URL; } if (server.equals("help")) { usage(); return; } try { Options options = new Options.Builder().server(server).noReconnect().build(); Connection nc = Nats.connect(options); Future<Message> replyFuture = nc.request("stan.time", null); Message reply = replyFuture.get(); System.out.printf("The time where stan is, is \"%s\"\n", new String(reply.getData(), StandardCharsets.UTF_8)); nc.close(); } catch (Exception exp) { exp.printStackTrace(); } }
Example 7
Source File: RequestTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testCleanupTimerWorks() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false)) { long cleanupInterval = 50; Options options = new Options.Builder().server(ts.getURI()).requestCleanupInterval(Duration.ofMillis(cleanupInterval)).build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Future<Message> incoming = nc.request("subject", null); incoming.cancel(true); incoming = nc.request("subject", null); incoming.cancel(true); incoming = nc.request("subject", null); incoming.cancel(true); Thread.sleep(2 * cleanupInterval); assertEquals(0, ((NatsStatistics)nc.getStatistics()).getOutstandingRequests()); // Make sure it is still running incoming = nc.request("subject", null); incoming.cancel(true); incoming = nc.request("subject", null); incoming.cancel(true); incoming = nc.request("subject", null); incoming.cancel(true); Thread.sleep(2 * cleanupInterval); assertEquals(0, ((NatsStatistics)nc.getStatistics()).getOutstandingRequests()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
Example 8
Source File: SubscriptionImpl.java From stan.java with Apache License 2.0 | 4 votes |
@Override public void close(boolean unsubscribe) throws IOException { StreamingConnectionImpl sc; String reqSubject; Connection nc; wLock(); try { sc = this.sc; if (sc == null) { throw new IllegalStateException(NatsStreaming.ERR_BAD_SUBSCRIPTION); } Dispatcher d = sc.getDispatcherByName(this.getOptions().getDispatcherName()); d.unsubscribe(this.inbox); this.sc = null; } finally { wUnlock(); } sc.lock(); try { // Snapshot connection to avoid data race, since the connection may be // closing while we try to send the request nc = sc.getNatsConnection(); if (nc == null) { throw new IllegalStateException(NatsStreaming.ERR_CONNECTION_CLOSED); } sc.subMap.remove(this.inbox); reqSubject = sc.subCloseRequests; if (unsubscribe) { reqSubject = sc.unsubRequests; } if (reqSubject.isEmpty()) { throw new IllegalStateException(ERR_NO_SERVER_SUPPORT); } } finally { sc.unlock(); } byte[] bytes; UnsubscribeRequest usr = UnsubscribeRequest.newBuilder() .setClientID(sc.getClientId()).setSubject(subject).setInbox(ackInbox).build(); bytes = usr.toByteArray(); io.nats.client.Message reply; try { Future<io.nats.client.Message> incoming = nc.request(reqSubject, bytes); reply = incoming.get(sc.opts.getConnectTimeout().toMillis(), TimeUnit.MILLISECONDS); if (reply == null) { if (unsubscribe) { throw new IOException(ERR_UNSUB_REQ_TIMEOUT); } throw new IOException(ERR_CLOSE_REQ_TIMEOUT); } } catch (TimeoutException|ExecutionException|InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } SubscriptionResponse response = SubscriptionResponse.parseFrom(reply.getData()); if (!response.getError().isEmpty()) { throw new IOException(PFX + response.getError()); } }