@GET @Path("{warName}") public Response deploymentState(@PathParam("warName") String warName) throws Exception { DeploymentCheckRequest request = new DeploymentCheckRequest(); request.setWarName(warName); Message<DeploymentCheckRequest> msg = new Message<DeploymentCheckRequest>(DeploymentCheckCommandAction.COMMAND_ACTION, request); checkResponse.resetAll(); sender.sendMessage(msg, null); Map<ChannelMember, DeploymentCheckResponse> responses = new HashMap<ChannelMember, DeploymentCheckResponse>(); int timeout = 240; while (timeout-- > 0) { Thread.sleep(500l); boolean foundAll = true; for (ChannelMember mem : members) { Message<DeploymentCheckResponse> memResponse = checkResponse.getResponse(mem); if (memResponse != null) { responses.put(mem, memResponse.getBody()); } else { foundAll = false; } } if (foundAll) { break; } } if (responses.size() == members.size()) { Set<ChannelMember> membersInError = new HashSet<ChannelMember>(); boolean combinedState = true; boolean anyStarted = false; for (ChannelMember mem : responses.keySet()) { DeploymentCheckResponse response = responses.get(mem); if (response.getError() != null) { combinedState = false; membersInError.add(mem); } else { combinedState = combinedState && response.isFinished(); } if (response.isStarted()) { anyStarted = true; } } Map<String, Object> responseObj = new LinkedHashMap<String, Object>(); responseObj.put("finished", combinedState); responseObj.put("started", anyStarted); if (membersInError.size() > 0) { Set<String> inError = new TreeSet<String>(); responseObj.put("errors", inError); for (ChannelMember mem : membersInError) { inError.add(mem.getAddress().toString() + (mem.isCoordinator() ? ":coord" : "")); } } return Response.ok(new Gson().toJson(responseObj)).build(); } return Response.status(Response.Status.NOT_FOUND).build(); }
@POST @Path("/findVineParallel") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Vine findVineParallelFromImage(InputStream uploadedInputStream) { // Thread thread = null; // try { // thread = factory.newThread(new ParallelThread(1 , new // ImageFeatures2D(ImageIO.read(uploadedInputStream), false), // serviceVineDescribed.getVineDescribeds())); // } catch (IOException e) { // e.printStackTrace(); // } // thread.start(); try { // Get the default ManagedThreadFactory implementation. InitialContext ctx = new InitialContext(); ManagedThreadFactory factory = (ManagedThreadFactory) ctx.lookup("java:comp/DefaultManagedThreadFactory"); // Create a new thread using the thread factory created above. Thread myThread = factory.newThread( new Runnable() { @Override public void run() { System.out.println("Running a task using Managed thread ..."); } }); // Start executing the thread. myThread.start(); } catch (NamingException e) { e.printStackTrace(); } return null; }
@Path("/rd") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(SseFeature.SERVER_SENT_EVENTS) public EventOutput respondToRegisterDevice( String test, @QueryParam("ep") final String endPoint, @QueryParam("lt") long lt, @QueryParam("sms") String sms, @QueryParam("lwm2m") String lwm2mVersion, @QueryParam("b") String bindingMode) throws Exception { ObjectMapper mapper = new ObjectMapper(); final ClientObject clientObject = mapper.readValue(test, ClientObject.class); final Datastore ds = MongoConnection.getServer(); final Query<ClientObject> query = ds.createQuery(ClientObject.class).filter("client_bs_obj.device_id =", endPoint); final List<ClientObject> clientObjects = query.asList(); final EventOutput eventOutput = new EventOutput(); Thread rdThread = new Thread( new Runnable() { public void run() { try { final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder(); if (clientObjects.size() == 0) { ds.save(clientObject); eventBuilder.name("register-message-to-client"); eventBuilder.data(String.class, "Register success!"); final OutboundEvent event = eventBuilder.build(); eventOutput.write(event); } else { ds.updateFirst(query, clientObject, true); eventBuilder.name("register-message-to-client"); eventBuilder.data(String.class, "Update!"); final OutboundEvent event = eventBuilder.build(); eventOutput.write(event); } while (true) { final OutboundEvent.Builder eventBuilder1 = new OutboundEvent.Builder(); eventBuilder1.name("register-message-to-client"); // wait for a signal CountDownLatch signal = new CountDownLatch(1); Info.setCountDownLatchMap(endPoint, signal); signal.await(); System.out.println(endPoint); System.out.println("hello"); eventBuilder1.data(String.class, Info.getInfo(endPoint)); final OutboundEvent event = eventBuilder1.build(); eventOutput.write(event); signal = new CountDownLatch(1); Info.setCountDownLatchMap(endPoint, signal); // Info.setInfo(endPoint, null); } } catch (IOException e) { throw new RuntimeException("Bad Request", e); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { eventOutput.close(); } catch (IOException ioClose) { throw new RuntimeException("Error when closing the event output.", ioClose); } } } }); rdThread.start(); return eventOutput; }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces("text/plain") public String deploy( DeployRequest req, @HeaderParam("Authorization") @DefaultValue("no token") String auth, @Context ServletContext context) throws Exception { if (!this.isAuth(auth)) { throw new Exception("Unauthorized!"); } String contextRoot = req.getContextRoot(); String artifact = req.getArtifact(); if (StringUtils.isEmptyOrNull(req.getRepo()) || StringUtils.isEmptyOrNull(req.getDomain())) { return "Error: missing repo and/or domain."; } if (StringUtils.isEmptyOrNull(contextRoot)) { contextRoot = "/"; } if (StringUtils.isEmptyOrNull(artifact)) { artifact = "com.meltmedia.cadmium:cadmium-war:war:" + version; } if (StringUtils.isEmptyOrNull(req.getConfigRepo())) { req.setConfigRepo(req.getRepo()); } if (StringUtils.isEmptyOrNull(req.getConfigBranch())) { req.setConfigBranch("master"); } if (StringUtils.isEmptyOrNull(req.getBranch())) { req.setBranch("master"); } ChannelMember coordinator = membershipTracker.getCoordinator(); com.meltmedia.cadmium.deployer.DeployRequest mRequest = new com.meltmedia.cadmium.deployer.DeployRequest(); mRequest.setBranch(req.getBranch()); mRequest.setRepo(req.getRepo()); mRequest.setConfigBranch(req.getConfigBranch()); mRequest.setConfigRepo(req.getConfigRepo()); mRequest.setDomain(req.getDomain()); mRequest.setContext(contextRoot); mRequest.setSecure(!req.isDisableSecurity()); mRequest.setArtifact(artifact); Message<com.meltmedia.cadmium.deployer.DeployRequest> msg = new Message<com.meltmedia.cadmium.deployer.DeployRequest>( DeployCommandAction.DEPLOY_ACTION, mRequest); response.reset(coordinator); sender.sendMessage(msg, coordinator); int timeout = 4800; while (timeout-- > 0) { Thread.sleep(500l); Message<DeployResponse> returnMsg = response.getResponse(coordinator); if (returnMsg != null) { if (returnMsg.getBody().getError() != null) { throw new Exception(returnMsg.getBody().getError()); } return returnMsg.getBody().getWarName(); } } return "ok"; }