@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; }