Java Code Examples for org.osgi.service.event.EventAdmin#sendEvent()

The following examples show how to use org.osgi.service.event.EventAdmin#sendEvent() . 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: LogAppenderTest.java    From karaf-decanter with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-log", new RolePrincipal("admin")));

    // send event synchronously for test
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    // get log
    String log = executeCommand("log:display");

    Assert.assertTrue(log.contains("foo=bar"));
    Assert.assertTrue(log.contains("decanter/collect/test"));
}
 
Example 2
Source File: FileAppenderTest.java    From karaf-decanter with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-file", new RolePrincipal("admin")));

    // send event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    // read file
    File file = new File(System.getProperty("karaf.data"), "decanter");
    StringBuilder builder = new StringBuilder();
    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
        builder.append(reader.readLine()).append("\n");
    }

    Assert.assertTrue(builder.toString().contains("foo=bar"));
}
 
Example 3
Source File: KafkaAppenderTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
/**
 * Require running Kafka broker.
 */
@Test
public void test() throws Exception {
    System.out.println("Please, first download and start Kafka:");
    System.out.println("\tbin/zookeeper-server-start.sh ../config/zookeeper.properties");
    System.out.println("\tbin/kafka-server-start.sh ../config/server.properties");
    System.out.println("Create decanter topic:");
    System.out.println("\tbin/kafka-topics.sh --create --partitions 1 --replication-factor 1 --zookeeper localhost:2181 --topic decanter");

    // install decanter
    addFeaturesRepository("mvn:org.apache.karaf.decanter/apache-karaf-decanter/" + System.getProperty("decanter.version") + "/xml/features");
    installAndAssertFeature("decanter-common");
    installAndAssertFeature("decanter-appender-kafka");

    // send event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    System.out.println("Check message in Kafka using:");
    System.out.println("\tbin/kafka-console-consumer.sh ");

    System.out.println("*********************************");
    System.out.println("* Latest Tested Version: 2.1.0  *");
    System.out.println("*********************************");
}
 
Example 4
Source File: JmsAppenderTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    // install jms
    System.out.println(executeCommand("feature:install jms", new RolePrincipal("admin")));
    System.out.println(executeCommand("feature:install pax-jms-activemq", new RolePrincipal("admin")));

    // create connection factory
    System.out.println(executeCommand("jms:create decanter"));

    Thread.sleep(2000);

    System.out.println(executeCommand("jms:connectionfactories"));
    System.out.println(executeCommand("jms:info jms/decanter"));

    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-jms", new RolePrincipal("admin")));

    // send event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    // browse
    String browse = executeCommand("jms:browse jms/decanter decanter");

    System.out.println(browse);

    Assert.assertTrue(browse.contains("\"foo\":\"bar\""));
}
 
Example 5
Source File: WebsocketAppenderTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-websocket-servlet", new RolePrincipal("admin")));

    String httpList = executeCommand("http:list");
    while (!httpList.contains("Deployed")) {
        Thread.sleep(500);
        httpList = executeCommand("http:list");
    }
    System.out.println(httpList);

    // websocket
    WebSocketClient client = new WebSocketClient();
    DecanterSocket decanterSocket = new DecanterSocket();
    client.start();
    URI uri = new URI("ws://localhost:" + getHttpPort() + "/decanter-websocket");
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    client.connect(decanterSocket, uri, request).get();

    // sending event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    decanterSocket.awaitClose(20, TimeUnit.SECONDS);

    Assert.assertEquals(1, decanterSocket.messages.size());

    Assert.assertTrue(decanterSocket.messages.get(0).contains("\"foo\":\"bar\""));
    Assert.assertTrue(decanterSocket.messages.get(0).contains("\"event_topics\":\"decanter/collect/test\""));

    client.stop();
}
 
Example 6
Source File: InfluxdbAppenderTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
/**
 * Require a running InfluxDB instance.
 */
@Test
public void test() throws Exception {
    System.out.println("Please first download and start InfluxDB instance with:");
    System.out.println("\tusr/bin/influxd");
    System.out.println("Create the database and default replication policy:");
    System.out.println("\tusr/bin/influx");
    System.out.println("\t> create database decanter");
    System.out.println("\t> use decanter");
    System.out.println("\t> create retention policy \"defaultPolicy\" on \"decanter\" duration 1d replication 1 default");

    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-influxdb", new RolePrincipal("admin")));

    // send event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    // check in InfluxDB
    System.out.println("Please check InfluxDB content with:");
    System.out.println("\tusr/bin/influx");
    System.out.println("\t> select * from decanter");


    System.out.println("**********************************");
    System.out.println("* Latest Tested Version: 1.7.9-1 *");
    System.out.println("**********************************");
}
 
Example 7
Source File: CassandraAppenderTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
/**
 * Require a running Cassandra instance.
 */
@Test
public void test() throws Exception {
    System.out.println("Please, first download and run Cassandra");
    System.out.println("\t* Start cassandra with bin/cassandra -f");
    System.out.println("\t* Connect using bin/cqlsh and do:");
    System.out.println("\t    cqlsh> CREATE KEYSPACE IF NOT EXISTS decanter WITH REPLICATION = { 'class': 'SimpleStrategy', 'replication_factor': 1 };");
    System.out.println("");

    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-cassandra", new RolePrincipal("admin")));

    // send event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    // check in Cassandra
    System.out.println("Please check in decanter table in Cassandra with bin/cqlsh:");
    System.out.println("\t cqlsh> SELECT * FROM decanter.decanter");
    System.out.println("");

    System.out.println("*********************************");
    System.out.println("* Latest Tested Version: 3.11.5 *");
    System.out.println("*********************************");
}
 
Example 8
Source File: JdbcAppenderTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    // install database
    System.out.println(executeCommand("feature:install jdbc", new RolePrincipal("admin")));
    System.out.println(executeCommand("feature:install pax-jdbc-derby", new RolePrincipal("admin")));

    System.out.println(executeCommand("jdbc:ds-list"));

    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-jdbc", new RolePrincipal("admin")));

    // send event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    // check database content
    DataSource dataSource = getOsgiService(DataSource.class);
    try (Connection connection = dataSource.getConnection()) {
        try (Statement statement = connection.createStatement()) {
            try (ResultSet resultSet = statement.executeQuery("select * from decanter")) {
                resultSet.next();
                String json = resultSet.getString(2);
                Assert.assertTrue(json.contains("\"foo\":\"bar\""));
                Assert.assertTrue(json.contains("\"event_topics\":\"decanter/collect/test\""));
            }
        }
    }
}
 
Example 9
Source File: MongodbAppenderTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
/**
 * Require a running MongoDB instance.
 */
@Test
public void test() throws Exception {
    System.out.println("Download and start MongoDB with:");
    System.out.println("\tbin/mongod");
    System.out.println("Create a database in MongoDB with:");
    System.out.println("\tbin/mongo");
    System.out.println("\t> use decanter");
    System.out.println("");
    System.out.println("Installing Decanter");
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-common", new RolePrincipal("admin")));
    System.out.println(executeCommand("feature:install decanter-appender-mongodb", new RolePrincipal("admin")));

    System.out.println("Sending Decanter event");
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, Object> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    System.out.println("Check data in MongoDB with:");
    System.out.println("\tmongo");
    System.out.println("\t> use decanter");
    System.out.println("\t> db.decanter.find()");

    System.out.println("*********************************");
    System.out.println("* Latest Tested Version: 4.2.1  *");
    System.out.println("*********************************");
}
 
Example 10
Source File: ElasticsearchAppenderTest.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
/**
 * Require a running Elasticsearch instance.
 */
@Test
public void test() throws Exception {
    System.out.println("Please first download and start Elasticsearch instance with:");
    System.out.println("\tbin/elasticsearch");

    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-elasticsearch", new RolePrincipal("admin")));

    // send event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    // check in Cassandra
    System.out.println("Please check in karaf index in Elasticsearch with:");
    System.out.println("\t curl http://localhost:9200/_cat/indices");
    System.out.println("\t curl http://localhost:9200/karaf-yyyy.mm.dd");
    System.out.println("");


    System.out.println("********************************");
    System.out.println("* Latest Tested Version: 7.5.0 *");
    System.out.println("********************************");
}
 
Example 11
Source File: Scenario13TestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void runTest() throws Throwable {
  /* Claims the reference of the EventAdmin Service */
  serviceReference = bundleContext
    .getServiceReference(EventAdmin.class.getName());

  /* assert that a reference is aquired */
  assertNotNull(getName()
                + " Should be able to get reference to EventAdmin service",
                serviceReference);
  /* check the service reference */
  if (serviceReference == null) {
    /* set fail */
    fail(getName() + " service reference should not be null");
  }

  /* get the service  */
  eventAdmin = (EventAdmin) bundleContext
    .getService(serviceReference);

  /* assert that service is available */
  assertNotNull(getName()
                + " Should be able to get instance to EventAdmin object",eventAdmin);

  /* check if null */
  if (eventAdmin == null) {
    /* set a fail */
    fail(getName() + " event admin should not be null");
  }

  /* create an anonymous thread */
  Thread synchDeliver = new Thread() {
      public void run() {
        /* deliver the messages */
        for (int i = 0; i < messageTosend; i++) {
          /* a Hash table to store message in */
          Dictionary message = new Hashtable();
          /* put some properties into the messages */
          message.put("Synchronus message",new Integer(i));
          /* send the message */
          eventAdmin.sendEvent(new Event("com/acme/timer", message));

        }
      }


    };

  /* print that the test has started */
  System.out.println("Testing synchronus delivery");
  /* start the thread */
  synchDeliver.start();

  /* wait until thread is dead */
  synchDeliver.join();
  /* unget the service */
  bundleContext.ungetService(serviceReference);
}
 
Example 12
Source File: Scenario7TestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void runTest() throws Throwable {
  /* determine if to use a local copy of EventAdmin or not */
  /* Claims the reference of the EventAdmin Service */
  serviceReference = bundleContext
    .getServiceReference(EventAdmin.class.getName());

  /* assert that a reference is aquired */
  assertNotNull(getName()
                + " Should be able to get reference to EventAdmin service",
                serviceReference);

  if (serviceReference == null) {
    fail(getName() + " service reference should not be null");
  }

  eventAdmin = (EventAdmin) bundleContext
    .getService(serviceReference);

  assertNotNull(getName()
                + " Should be able to get instance to EventAdmin object");

  if (eventAdmin == null) {
    fail(getName() + " event admin should not be null");
  }

  stopped = false;

  synchDeliver = new Thread() {
      public void run() {
        int i = 0;
        while (!stopped && !Thread.interrupted()) {
          try {
            /* a Hash table to store message in */
            Dictionary message = new Hashtable();
            /* put some properties into the messages */
            message.put("Synchronus message", new Integer(i));
            /* send the message */
            System.out
              .println(getName()
                       + " sending a synchronus event with message:"
                       + message.toString()
                       + "and the topic:" + topicToSend);
            eventAdmin
              .sendEvent(new Event(topicToSend, message));
            /* Puts the thread to sleep for 10 seconds */
            i++;
            Thread.sleep(10000);
          } catch (InterruptedException e) {
            //ignored, treated by while loop
          }
        }
      }
    };

  synchDeliver.start();

  asynchDeliver = new Thread() {
      public void run() {
        int i = 0;
        while (!stopped && !Thread.interrupted()) {
          try {
            /* a Hash table to store message in */
            Dictionary message = new Hashtable();
            /* put some properties into the messages */
            message.put("Asynchronus message", new Integer(i));
            /* send the message */
            System.out
              .println(getName()
                       + " sending a asynchronus event with message:"
                       + message.toString()
                       + "and the topic:" + topicToSend);
            eventAdmin
              .sendEvent(new Event(topicToSend, message));
            /* Puts the thread to sleep for 10 seconds */
            i++;
            Thread.sleep(10000);
          } catch (InterruptedException e) {
            //ignored, treated by while loop
          }
        }
      }
    };
  asynchDeliver.start();

}
 
Example 13
Source File: CamelAppenderTest.java    From karaf-decanter with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
    final List<Exchange> exchanges = new ArrayList<>();
    // create route
    RouteBuilder routeBuilder = new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct-vm:decanter").process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    exchanges.add(exchange);
                }
            });
        }
    };
    DefaultCamelContext camelContext = new DefaultCamelContext();
    camelContext.setName("context-test");
    camelContext.addRoutes(routeBuilder);
    camelContext.start();

    Thread.sleep(1000);

    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-camel", new RolePrincipal("admin")));

    Thread.sleep(2000);

    // send event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    Assert.assertEquals(1, exchanges.size());

    HashMap<String, Object> received = exchanges.get(0).getIn().getBody(HashMap.class);
    Assert.assertEquals("decanter/collect/test", received.get("event.topics"));
    Assert.assertEquals("bar", received.get("foo"));
}
 
Example 14
Source File: RestAppenderTest.java    From karaf-decanter with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
    List<String> calls = new ArrayList<>();
    // register servlet
    httpService.registerServlet("/test", new HttpServlet() {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            try (BufferedReader reader = new BufferedReader(req.getReader())) {
                String line;
                while ((line = reader.readLine()) != null) {
                    calls.add(line);
                }
            }
            resp.setStatus(200);
            try (BufferedWriter writer = new BufferedWriter(resp.getWriter())) {
                writer.write("DONE");
            }
        }
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // nothing to do
        }
    }, null, null);

    System.out.println(executeCommand("http:list"));

    // configure appender
    File file = new File(System.getProperty("karaf.etc"), "org.apache.karaf.decanter.appender.rest.cfg");
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
        writer.write("uri=http://localhost:" + getHttpPort() + "/test\n");
        writer.write("marshaller.target=(dataFormat=json)");
    }

    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-rest", new RolePrincipal("admin")));

    Thread.sleep(2000);

    // send event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    Assert.assertEquals(1, calls.size());

    Assert.assertTrue(calls.get(0).contains("\"foo\":\"bar\""));
    Assert.assertTrue(calls.get(0).contains("\"event_topics\":\"decanter/collect/test\""));
}
 
Example 15
Source File: SocketAppenderTest.java    From karaf-decanter with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
    List<String> received = new ArrayList<>();

    // create server socket
    ServerSocket serverSocket = new ServerSocket(34343);
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    Socket socket = serverSocket.accept();
                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                        String line;
                        while ((line = reader.readLine()) != null) {
                            received.add(line);
                        }
                    }
                } catch (Exception e) {
                    System.err.println(e);
                }
            }
        }
    };
    Thread thread = new Thread(runnable);
    thread.start();

    // install decanter
    System.out.println(executeCommand("feature:repo-add decanter " + System.getProperty("decanter.version")));
    System.out.println(executeCommand("feature:install decanter-appender-socket", new RolePrincipal("admin")));

    Thread.sleep(2000);

    // send event
    EventAdmin eventAdmin = getOsgiService(EventAdmin.class);
    HashMap<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    Event event = new Event("decanter/collect/test", data);
    eventAdmin.sendEvent(event);

    Assert.assertEquals(1, received.size());

    Assert.assertTrue(received.get(0).contains("\"foo\":\"bar\""));
    Assert.assertTrue(received.get(0).contains("\"event_topics\":\"decanter/collect/test\""));
}