-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
635 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
envs/se/src/main/java/org/apache/camel/cdi/se/bean/EventConsumingRoute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.camel.cdi.se.bean; | ||
|
||
import org.apache.camel.Endpoint; | ||
import org.apache.camel.builder.RouteBuilder; | ||
import org.apache.camel.cdi.CdiEvent; | ||
import org.apache.camel.cdi.Uri; | ||
import org.apache.camel.component.mock.MockEndpoint; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class EventConsumingRoute extends RouteBuilder { | ||
|
||
@Inject | ||
@Uri(CdiEvent.CDI_EVENT_URI) | ||
private Endpoint allCdiEventsEndpoint; | ||
|
||
@Inject | ||
@Uri("mock:resultAll") | ||
private MockEndpoint resultAllEndpoint; | ||
|
||
@Inject | ||
@Uri("mock:resultBean") | ||
private MockEndpoint resultBeanEndpoint; | ||
|
||
@Inject | ||
@Uri("mock:resultString") | ||
private MockEndpoint resultStringEndpoint; | ||
|
||
@Override | ||
public void configure() { | ||
from(allCdiEventsEndpoint).to(resultAllEndpoint); | ||
|
||
from(CdiEvent.endpoint(SampleBean.class)) | ||
.to(resultBeanEndpoint); | ||
|
||
from(CdiEvent.endpoint(String.class)) | ||
.to(resultStringEndpoint); | ||
|
||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
envs/se/src/main/java/org/apache/camel/cdi/se/bean/SampleBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.apache.camel.cdi.se.bean; | ||
|
||
public class SampleBean { | ||
|
||
protected String label; | ||
|
||
public SampleBean(String label) { | ||
this.label = label; | ||
} | ||
|
||
public String getLabel() { | ||
return label; | ||
} | ||
|
||
public void setLabel(String label) { | ||
this.label = label; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
SampleBean that = (SampleBean) o; | ||
|
||
if (!label.equals(that.label)) return false; | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return label.hashCode(); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
envs/se/src/test/java/org/apache/camel/cdi/se/EventComponentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.camel.cdi.se; | ||
|
||
import org.apache.camel.CamelContext; | ||
import org.apache.camel.ProducerTemplate; | ||
import org.apache.camel.cdi.CdiCamelExtension; | ||
import org.apache.camel.cdi.CdiEvent; | ||
import org.apache.camel.cdi.Uri; | ||
import org.apache.camel.cdi.se.bean.EventConsumingRoute; | ||
import org.apache.camel.cdi.se.bean.SampleBean; | ||
import org.apache.camel.component.mock.MockEndpoint; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.arquillian.junit.InSequence; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import javax.enterprise.event.Event; | ||
import javax.inject.Inject; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; | ||
|
||
@RunWith(Arquillian.class) | ||
public class EventComponentTest { | ||
|
||
@Deployment | ||
public static Archive<?> deployment() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
// Camel CDI | ||
.addPackage(CdiCamelExtension.class.getPackage()) | ||
// Test class | ||
.addClass(EventConsumingRoute.class) | ||
// Bean archive deployment descriptor | ||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); | ||
} | ||
|
||
@Inject | ||
@Uri(CdiEvent.CDI_EVENT_URI) | ||
private ProducerTemplate inbound; | ||
|
||
@Inject | ||
Event<SampleBean> sampleBeanEvent; | ||
|
||
@Inject | ||
@Uri("mock:resultAll") | ||
private MockEndpoint outboundAll; | ||
|
||
@Inject | ||
@Uri("mock:resultBean") | ||
private MockEndpoint outboundBean; | ||
|
||
@Inject | ||
@Uri("mock:resultString") | ||
private MockEndpoint outboundString; | ||
|
||
@Test | ||
@InSequence(1) | ||
public void startCamelContext(CamelContext context, List<Class> events) throws Exception { | ||
context.start(); | ||
} | ||
|
||
@Test | ||
@InSequence(2) | ||
public void sendMessageToInbound(List<Class> events) throws InterruptedException { | ||
outboundAll.expectedMessageCount(4); | ||
outboundAll.expectedBodiesReceived(1234, new SampleBean("foo"), new SampleBean("bar"), "test"); | ||
|
||
outboundBean.expectedMessageCount(2); | ||
outboundBean.expectedBodiesReceived(new SampleBean("foo"), new SampleBean("bar")); | ||
|
||
outboundString.expectedMessageCount(1); | ||
outboundString.expectedBodiesReceived("test"); | ||
|
||
inbound.sendBody(1234); | ||
inbound.sendBody(new SampleBean("foo")); | ||
sampleBeanEvent.fire(new SampleBean("bar")); | ||
inbound.sendBody("test"); | ||
|
||
assertIsSatisfied(2L, TimeUnit.SECONDS, outboundAll); | ||
assertIsSatisfied(2L, TimeUnit.SECONDS, outboundBean); | ||
assertIsSatisfied(2L, TimeUnit.SECONDS, outboundString); | ||
} | ||
|
||
@Test | ||
@InSequence(3) | ||
public void stopCamelContext(CamelContext context, List<Class> events) throws Exception { | ||
context.stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.camel.cdi; | ||
|
||
/** | ||
* <p> | ||
* Consume all CDI events with this endpoint and route them to Camel dynamically, | ||
* based on event (payload) type. Additional qualifiers are currently not supported, | ||
* only the desired event (payload) type can be constrained. | ||
* </p> | ||
* <p> | ||
* Produce and fire CDI events in Camel exchanges with this destination endpoint. | ||
* The exchange input message body is the event type and payload. You will get an | ||
* exception if the declared type of the producing endpoint doesn't match the | ||
* message payload. An <code>Annotation[]</code> of additional qualifiers | ||
* can be added as message header {@link #CDI_EVENT_QUALIFIERS}. | ||
* </p> | ||
* <p> | ||
* Usage: <code>cdi-event://some.event.CustomType</code> or all with <code>cdi-event:///</code> | ||
* </p> | ||
* <p> | ||
* Alternatively, call {@link CdiEvent#endpoint(Class)} to generate the string. | ||
* </p> | ||
* <p> | ||
* TODO: Support <code>@Inject @Uri(CDI_EVENT_URI) @SomeQualifier Endpoint|ProducerTemplate</code> | ||
* </p> | ||
* | ||
* @author Christian Bauer | ||
*/ | ||
public final class CdiEvent { | ||
|
||
public static final String CDI_EVENT = "cdi-event"; | ||
public static final String CDI_EVENT_URI = CDI_EVENT + ":///"; | ||
public static final String CDI_EVENT_QUALIFIERS = "CamelCdiQualifiers"; | ||
|
||
public static String endpoint() { | ||
return endpoint(null); | ||
} | ||
|
||
public static String endpoint(Class<?> clazz) { | ||
return CDI_EVENT + "://" + (clazz != null ? clazz.getName() : "") + "/"; | ||
} | ||
|
||
} |
Oops, something went wrong.