Có thiết lập Luồng công việc sau đây trong ServletContextListener trong một dự án Web chạy trên IBM WebSphere, một XML đến được chuyển đổi thành JSON và được in ra System.out và được in ra report.txt. Càng xa càng tốt.Cách gọi một cuộc gọi REST (POST với JSON body) từ Camel trong Java DSL
@WebListener
public class SetupCamel implements ServletContextListener {
private CamelContext camelContext;
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("SetupCamel:contextInitialized - enter");
try {
Context ctx = new InitialContext();
QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("jms/TestConnectionFactory");
camelContext = new DefaultCamelContext();
JmsConfiguration jmsConfiguration = new JmsConfiguration(qcf);
JmsComponent jmsComponent = new JmsComponent(jmsConfiguration);
camelContext.addComponent("jms", jmsComponent);
final XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setEncoding("UTF-8");
xmlJsonFormat.setForceTopLevelObject(false);
xmlJsonFormat.setTrimSpaces(true);
xmlJsonFormat.setRootName("newRoot");
xmlJsonFormat.setSkipNamespaces(true);
xmlJsonFormat.setRemoveNamespacePrefixes(true);
camelContext.addRoutes(new RouteBuilder() {
public void configure() {
onException(Exception.class)
.to("log:GeneralError?level=ERROR")
.end();
from("jms:queue:TestQueue?concurrentConsumers=1")
.marshal(xmlJsonFormat)
.to("file:/tmp/messages?fileName=report.txt&fileExist=Append")
.to("stream:out")
;
}
});
camelContext.start();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("SetupCamel:contextInitialized - leaving");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("SetupCamel:contextDestroyed - enter");
try {
if (camelContext != null) {
camelContext.stop();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("SetupCamel:contextDestroyed - leaving");
}
}
Tôi phải mở rộng luồng để POST JSON lên dịch vụ REST/người tiêu dùng. (Tài nguyên còn lại được kiểm tra và hoạt động ..)
Tài liệu tìm kiếm (web) không cung cấp cho tôi một ví dụ Java DSL hoàn chỉnh/tốt mà tôi có thể sử dụng. Dựa trên những gì tôi đã thấy, tôi nghĩ rằng nó nên được một cái gì đó tương tự để thêm một thiết bị đầu cuối như:
.to("cxfrs://http://localhost:9080/WebContext/TestResource")
Nhưng điều này không làm việc và tôi không hiểu làm thế nào để thiết lập chuyển đổi JSON như cơ thể và làm cho nó một yêu cầu POST. Cũng không có ngoại lệ được in.
Làm cách nào để thêm cuộc gọi REST dưới dạng POST với cơ thể JSON trong luồng này?
Chạy trong IBM WebSphere v8.5.5, IBM jdk 1.7 lần, Camel 2.11.2
Các file jar sau nằm trong thư mục WEB-INF/lib classpath:
camel-core-2.11.2.jar
camel-cxf-2.11.2.jar
camel-cxf-transport-2.11.2.jar
camel-jms-2.11.2.jar
camel-servletlistener-2.11.2.jar
camel-spring-2.11.2.jar
camel-stream-2.11.2.jar
camel-xmljson-2.11.2.jar
com.ibm.ws.prereq.jackson.jar
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
cxf-api-2.7.6.jar
cxf-rt-frontend-jaxrs-2.7.6.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar
slf4j-api-1.7.5.jar
spring-beans-3.1.4.RELEASE.jar
spring-context-3.1.4.RELEASE.jar
spring-core-3.1.4.RELEASE.jar
spring-jms-3.1.4.RELEASE.jar
spring-tx-3.1.4.RELEASE.jar
xom-1.2.5.jar
Cảm ơn.
Bạn có quản lý để làm điều này? – geekprogrammer