/** * Publish to a kafka topic * based on the connection information * @param arr */ public void publish(INDArray arr) { if (producerTemplate == null) producerTemplate = camelContext.createProducerTemplate(); producerTemplate.sendBody("direct:start", arr); }
/** * Receive an ndarray * @return */ public INDArray receive() { if (consumerTemplate == null) consumerTemplate = camelContext.createConsumerTemplate(); return consumerTemplate.receiveBody("direct:receive", INDArray.class); }
@Override public void process(Exchange exchange) throws Exception { final INDArray arr = (INDArray) exchange.getIn().getBody(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); Nd4j.write(arr, dos); byte[] bytes = bos.toByteArray(); String base64 = Base64.encodeBase64String(bytes); exchange.getIn().setBody(base64, String.class); String id = UUID.randomUUID().toString(); exchange.getIn().setHeader(KafkaConstants.KEY, id); exchange.getIn().setHeader(KafkaConstants.PARTITION_KEY, id); } }).to(kafkaUri);
/** * Program entry point */ public static void main(String[] args) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("stream:in").to("direct:greetings"); from("direct:greetings").to("stream:out"); } }); context.start(); context.getRoutes().forEach(r -> LOGGER.info(r.toString())); context.stop(); } }
@Override public void start() throws Exception { vertx.eventBus().consumer("announce", message -> { System.out.println("ANNOUNCE >> " + message.body()); }); vertx.eventBus().consumer("errors", message -> { System.out.println("ERROR >> " + message.body()); }); SimpleRegistry registry = new SimpleRegistry(); registry.put("filterService", new ReleasePostFilter()); CamelContext camelContext = new DefaultCamelContext(registry); camelContext.addRoutes(createMyRoutes()); camelContext.start(); CamelBridge.create(vertx, new CamelBridgeOptions(camelContext) .addInboundMapping(fromCamel("seda:announce").toVertx("announce")) .addInboundMapping(fromCamel("seda:errors").toVertx("errors"))) .start(); }
Content subjContent = new Content().withData("Test of Amazon SES"); Message msg = new Message().withSubject(subjContent); // Include a body in both text and HTML formats Content textContent = new Content().withData("Hello - I hope you're having a good day."); Content htmlContent = new Content().withData("<h1>Hello - I hope you're having a good day.</h1>"); Body body = new Body().withHtml(htmlContent).withText(textContent); msg.setBody(body);
Session session = Session.getInstance(props, null); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("mail@tovare.com"));; msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("tov.are.jacobsen@iss.no", false)); msg.setSubject("Heisann "+System.currentTimeMillis()); msg.setText("Med vennlig hilsennTov Are Jacobsen"); msg.setHeader("X-Mailer", "Tov Are's program"); msg.setSentDate(new Date()); SMTPTransport t = (SMTPTransport)session.getTransport("smtps"); t.connect("smtp.gmail.com", "admin@tovare.com", "<insert password here>"); t.sendMessage(msg, msg.getAllRecipients()); System.out.println("Response: " + t.getLastServerResponse()); t.close();
camelCtx = new DefaultCamelContext(); if (camelCtx.getStatus() == ServiceStatus.Started || camelCtx.getStatus() == ServiceStatus.Starting) throw new IgniteException("Failed to start Camel streamer (CamelContext already started or starting)."); endpoint = CamelContextHelper.getMandatoryEndpoint(camelCtx, endpointUri); consumer = endpoint.createConsumer(this); ServiceHelper.startServices(camelCtx, endpoint, consumer); ServiceHelper.stopAndShutdownServices(camelCtx, endpoint, consumer);
protected void process( Processor processor, String item, @Default("Processor.Size.LARGE") Size size, @Default("red") String color, @Default("1") int quantity) { processor.process(item, size, color, quantity); } public void report(@Default("Hello") String message) { System.out.println("Message: " + message); }
public static void main(String[] args) { List<Processor> processors = new ArrayList<>(); processors.add(new Processor(10, 3, false) { @Override protected void doProcess(int i) { System.out.println("Fizz" + "(" + i + ") 3%10"); } }); // add more processors here for(int i = 1; i < 100; i++){ for (Processor processor : processors) { processor.process(i); } } }
@PreDestroy public void stop() throws Exception { try { this.consumer.stop(); } catch (Exception ex) { log.error("Can't stop consumer template", ex); } try { this.producer.stop(); } catch (Exception ex) { log.error("Can't stop producer template", ex); } try { this.ctx.stop(); } catch (Exception ex) { log.error("Can't stop camel context template", ex); } }
/** * Stops the streamer. * * @throws IgniteException In cases if failed to stop the streamer. */ public void stop() throws IgniteException { // If the Camel Context is stopping or stopped, reject this call to stop. if (camelCtx.getStatus() == ServiceStatus.Stopped || camelCtx.getStatus() == ServiceStatus.Stopping) throw new IgniteException("Failed to stop Camel streamer (CamelContext already stopped or stopping)."); // Stop Camel services. try { ServiceHelper.stopAndShutdownServices(camelCtx, endpoint, consumer); } catch (Exception e) { throw new IgniteException("Failed to stop Camel streamer [errMsg=" + e.getMessage() + ']'); } U.log(log, "Stopped Camel streamer, formerly consuming from endpoint URI: " + endpointUri); }
/** * Accepts only release announce. */ public boolean isRelease(@Body SyndFeed feed) { SyndEntry firstEntry = (SyndEntry) feed.getEntries().get(0); return firstEntry.getTitle().toLowerCase().contains("release"); }
public class MessageAdapter implements JsonSerializer<Message> { @Override public JsonElement serialize(Message message, Type type, JsonSerializationContext jsc) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("message_id", message.getMessageId()); jsonObject.addProperty("message", message.getMessage()); jsonObject.addProperty("user", message.getUsers().getUsername()); jsonObject.addProperty("date", message.getDate().toString()); return jsonObject; } }
/** * Processes the incoming {@link Exchange} and adds the tuple(s) to the underlying streamer. * * @param exchange The Camel Exchange. */ @Override public void process(Exchange exchange) throws Exception { // Extract and insert the tuple(s). if (getMultipleTupleExtractor() == null) { Map.Entry<K, V> entry = getSingleTupleExtractor().extract(exchange); getStreamer().addData(entry); } else { Map<K, V> entries = getMultipleTupleExtractor().extract(exchange); getStreamer().addData(entries); } // If the user has set a response processor, invoke it before finishing. if (resProc != null) resProc.process(exchange); }