Search in sources :

Example 1 with RouteBuilder

use of io.fabric8.openshift.api.model.RouteBuilder in project fabric8 by jboss-fuse.

the class ContainerTest method testProfilerStrategy.

@Test
public void testProfilerStrategy() throws Exception {
    Profiler profiler = new Profiler();
    Breadcrumbs breadcrumbs = new Breadcrumbs();
    CamelContext context = new DefaultCamelContext();
    profiler.manage(context);
    breadcrumbs.manage(context);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:a").doTry().to("seda:polyglot").choice().when(body().isEqualTo("<hello/>")).to("seda:english").throwException(new Exception("Error processing exchange")).endChoice().when(body().isEqualTo("<hallo/>")).to("seda:dutch").delay(2).to("seda:german").endChoice().otherwise().to("seda:french").endDoTry().doCatch(Throwable.class).to("seda:errors");
            String[] eps = { "polyglot", "english", "dutch", "german", "french", "errors" };
            for (String s : eps) {
                from("seda:" + s).aggregate(constant("ok"), new BodyInAggregatingStrategy()).completionSize(3).to("mock:" + s);
            }
        }
    });
    context.start();
    final ProducerTemplate template = new DefaultProducerTemplate(context);
    template.start();
    final String[] values = { "<hello/>", "<hallo/>", "<bonjour/>" };
    final Random rnd = new Random();
    for (int i = 0; i < 100; i++) {
        template.sendBody("direct:a", values[rnd.nextInt(values.length)]);
    }
    profiler.reset();
    long t0 = System.nanoTime();
    int nbThreads = 10;
    final CountDownLatch latch = new CountDownLatch(nbThreads);
    for (int t = 0; t < nbThreads; t++) {
        new Thread() {

            public void run() {
                for (int i = 0; i < 1000; i++) {
                    template.sendBody("direct:a", values[rnd.nextInt(values.length)]);
                }
                latch.countDown();
            }
        }.start();
    }
    latch.await();
    long t1 = System.nanoTime();
    System.out.println("Total time: " + TimeUnit.MILLISECONDS.convert(t1 - t0, TimeUnit.NANOSECONDS));
    print(profiler.getStatistics());
    System.out.println();
    MBeanServer mbeanServer = context.getManagementStrategy().getManagementAgent().getMBeanServer();
    ObjectName on = context.getManagementStrategy().getManagementNamingStrategy().getObjectNameForCamelContext(context);
    String xml = (String) mbeanServer.invoke(on, "dumpRoutesStatsAsXml", new Object[] { false, true }, new String[] { "boolean", "boolean" });
    System.out.println(xml);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultProducerTemplate(org.apache.camel.impl.DefaultProducerTemplate) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) Breadcrumbs(io.fabric8.insight.camel.breadcrumb.Breadcrumbs) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultProducerTemplate(org.apache.camel.impl.DefaultProducerTemplate) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ObjectName(javax.management.ObjectName) Profiler(io.fabric8.insight.camel.profiler.Profiler) Random(java.util.Random) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 2 with RouteBuilder

use of io.fabric8.openshift.api.model.RouteBuilder in project wildfly-camel by wildfly-extras.

the class KubernetesIntegrationTest method testKubernetesProducer.

@Test
public void testKubernetesProducer() throws Exception {
    String kubernetesProducerURI = String.format("kubernetes:%s?category=pods&operation=createPod", getKubernetesMaster());
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to(kubernetesProducerURI);
        }
    });
    ProducerTemplate template = camelctx.createProducerTemplate();
    String podName = POD_NAME + "-producer";
    camelctx.start();
    KubernetesEndpoint kubernetesEndpoint = camelctx.getEndpoint(kubernetesProducerURI, KubernetesEndpoint.class);
    String kubernetesNamespace = getNamespace(kubernetesEndpoint.getKubernetesClient());
    try {
        Map<String, Object> headers = new HashMap<>();
        headers.put(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, kubernetesNamespace);
        headers.put(KubernetesConstants.KUBERNETES_POD_NAME, podName);
        headers.put(KubernetesConstants.KUBERNETES_POD_SPEC, createPodSpec());
        Pod pod = template.requestBodyAndHeaders("direct:start", null, headers, Pod.class);
        Assert.assertNotNull("Expected pod to be not null", pod);
        Assert.assertEquals("Pending", pod.getStatus().getPhase());
    } finally {
        // Clean up
        Map<String, Object> headers = new HashMap<>();
        headers.put(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, kubernetesNamespace);
        headers.put(KubernetesConstants.KUBERNETES_POD_NAME, podName);
        template.requestBodyAndHeaders("kubernetes:" + getKubernetesMaster() + "?category=pods&operation=deletePod", null, headers);
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) KubernetesEndpoint(org.apache.camel.component.kubernetes.KubernetesEndpoint) RouteBuilder(org.apache.camel.builder.RouteBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) HashMap(java.util.HashMap) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with RouteBuilder

use of io.fabric8.openshift.api.model.RouteBuilder in project fabric8-maven-plugin by fabric8io.

the class OpenShiftRouteEnricher method hasRoute.

/**
 * Returns true if we already have a route created for the given name
 */
private boolean hasRoute(final KubernetesListBuilder listBuilder, final String name) {
    final AtomicBoolean answer = new AtomicBoolean(false);
    listBuilder.accept(new TypedVisitor<RouteBuilder>() {

        @Override
        public void visit(RouteBuilder builder) {
            ObjectMeta metadata = builder.getMetadata();
            if (metadata != null && name.equals(metadata.getName())) {
                answer.set(true);
            }
        }
    });
    return answer.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) RouteBuilder(io.fabric8.openshift.api.model.RouteBuilder)

Example 4 with RouteBuilder

use of io.fabric8.openshift.api.model.RouteBuilder in project fabric8 by jboss-fuse.

the class ContainerTest method testLoadBalancer.

@Test
public void testLoadBalancer() throws Exception {
    Profiler profiler = new Profiler();
    Breadcrumbs breadcrumbs = new Breadcrumbs();
    CamelContext context = new DefaultCamelContext();
    profiler.manage(context);
    breadcrumbs.manage(context);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:a").loadBalance().failover().to("mock:out");
        }
    });
    context.start();
    final ProducerTemplate template = new DefaultProducerTemplate(context);
    template.start();
    template.sendBody("direct:a", "Hello");
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultProducerTemplate(org.apache.camel.impl.DefaultProducerTemplate) ProducerTemplate(org.apache.camel.ProducerTemplate) Profiler(io.fabric8.insight.camel.profiler.Profiler) RouteBuilder(org.apache.camel.builder.RouteBuilder) Breadcrumbs(io.fabric8.insight.camel.breadcrumb.Breadcrumbs) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultProducerTemplate(org.apache.camel.impl.DefaultProducerTemplate) Test(org.junit.Test)

Example 5 with RouteBuilder

use of io.fabric8.openshift.api.model.RouteBuilder in project fabric8 by fabric8io.

the class MockConfigurer method configure.

public static void configure() {
    Service service1 = new ServiceBuilder().withNewMetadata().withName("service1").endMetadata().withNewSpec().addNewPort().withProtocol("TCP").withPort(80).withNewTargetPort(9090).endPort().endSpec().build();
    Service service2 = new ServiceBuilder().withNewMetadata().withName("service2").endMetadata().withNewSpec().addNewPort().withProtocol("TCP").withPort(80).withNewTargetPort(8080).endPort().endSpec().build();
    Service service3 = new ServiceBuilder().withNewMetadata().withName("service3").endMetadata().withNewSpec().addNewPort().withProtocol("TCP").withPort(443).withNewTargetPort(443).endPort().withClusterIP("172.30.17.2").endSpec().build();
    Service multiport = new ServiceBuilder().withNewMetadata().withName("multiport").endMetadata().withNewSpec().addNewPort().withName("port1").withProtocol("TCP").withPort(8081).withNewTargetPort(8081).endPort().addNewPort().withName("port2").withProtocol("TCP").withPort(8082).withNewTargetPort(8082).endPort().addNewPort().withName("port3").withProtocol("TCP").withPort(8083).withNewTargetPort(8083).endPort().withClusterIP("172.30.17.2").endSpec().build();
    MOCK.expect().get().withPath("/api/v1/namespaces/default/services/service1").andReturn(200, service1).always();
    MOCK.expect().get().withPath("/api/v1/namespaces/default/services/service2").andReturn(200, service2).always();
    MOCK.expect().get().withPath("/api/v1/namespaces/default/services/service3").andReturn(200, service3).always();
    MOCK.expect().get().withPath("/api/v1/namespaces/default/services/multiport").andReturn(200, multiport).always();
    MOCK.expect().get().withPath("/api/v1/namespaces/default/services").andReturn(200, new ServiceListBuilder().withItems(service1, service2, service3, multiport).build()).always();
    MOCK.expect().get().withPath("/api/v1/namespaces/default/endpoints").andReturn(200, new EndpointsListBuilder().build()).always();
    MOCK.expect().get().withPath("/oapi/v1/namespaces/default/routes").andReturn(200, new RouteBuilder().build()).always();
    String masterUrl = MOCK.getServer().url("/").toString();
    System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, masterUrl);
}
Also used : ServiceListBuilder(io.fabric8.kubernetes.api.model.ServiceListBuilder) RouteBuilder(io.fabric8.openshift.api.model.RouteBuilder) Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) EndpointsListBuilder(io.fabric8.kubernetes.api.model.EndpointsListBuilder)

Aggregations

RouteBuilder (io.fabric8.openshift.api.model.RouteBuilder)3 CamelContext (org.apache.camel.CamelContext)3 ProducerTemplate (org.apache.camel.ProducerTemplate)3 RouteBuilder (org.apache.camel.builder.RouteBuilder)3 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)3 Test (org.junit.Test)3 Breadcrumbs (io.fabric8.insight.camel.breadcrumb.Breadcrumbs)2 Profiler (io.fabric8.insight.camel.profiler.Profiler)2 EndpointsListBuilder (io.fabric8.kubernetes.api.model.EndpointsListBuilder)2 Service (io.fabric8.kubernetes.api.model.Service)2 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)2 ServiceListBuilder (io.fabric8.kubernetes.api.model.ServiceListBuilder)2 DefaultProducerTemplate (org.apache.camel.impl.DefaultProducerTemplate)2 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1