Mastering Spring Boot 3 & Apache Camel: Real‑World ProducerTemplate Cases
This article presents a comprehensive Spring Boot 3 tutorial that integrates Apache Camel, explains the ProducerTemplate API, demonstrates route definitions, shows code examples for sending messages, file writing, bean processing, and AI chat integration, and includes test result screenshots.
1. Introduction
Apache Camel is an open‑source integration framework based on Enterprise Integration Patterns. It supports DSLs such as Java, XML, Groovy and YAML, and can connect to many protocols (HTTP, Kafka, JMS, etc.) via a unified API.
2. Practical Cases
2.1 ProducerTemplate Overview
Similar to JdbcTemplate, ProducerTemplate sends messages to Camel routes. The two most common methods are sendBody() (one‑way, inOnly) and requestBody() (request‑reply, inOut).
2.2 Environment Setup
Add the Camel Spring Boot starter dependency:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>4.12.0</version>
</dependency>If the application runs without spring-boot-starter-web, enable continuous run:
camel:
springboot:
main-run-controller: true2.3 Defining Routes
Extend RouteBuilder and define four routes:
@Component
public class CamelRoute extends RouteBuilder {
@Override
public void configure() {
// 1. Log and transform
from("direct:start").log("Received: ${body}").transform(simple("Hello ${body}"));
// 2. Write to file
from("direct:fileRoute").to("file://d:/data/temp?fileName=output.txt&fileExist=Append");
// 3. Call a bean
from("direct:beanRoute").bean(ProcessingBean.class, "process");
// 4. Chat with LangChain4j
from("direct:chat")
.to("langchain4j-chat:test?chatModel=#openAiChatModel&chatOperation=CHAT_SINGLE_MESSAGE_WITH_PROMPT");
}
}ProcessingBean Definition
@Component
public class ProcessingBean {
public String process(String input) {
return "ProcessingBean 处理消息: " + input.toUpperCase();
}
}2.4 Using ProducerTemplate in a Controller
Inject ProducerTemplate and expose endpoints:
@RestController
@RequestMapping("/router")
public class RouterController {
private final ProducerTemplate producerTemplate;
public RouterController(ProducerTemplate producerTemplate) {
this.producerTemplate = producerTemplate;
}
@GetMapping("/simple")
public String sendSimpleMessage(String message) {
return producerTemplate.requestBody("direct:start", message, String.class);
}
@GetMapping("/file")
public String sendToFile(String message) {
producerTemplate.sendBody("direct:fileRoute", message + "
");
return String.format("%s 成功追加到文件output.txt中", message);
}
@GetMapping("/bean")
public String sendToBean(String message) {
return producerTemplate.requestBody("direct:beanRoute", message, String.class);
}
@GetMapping("/chat")
public String chat(String message) {
return producerTemplate.requestBodyAndHeader(
"direct:chat", java.util.Collections.emptyMap(),
org.apache.camel.component.langchain4j.ChatHeaders.PROMPT_TEMPLATE, message, String.class);
}
}Test results are shown in the following screenshots:
2.5 AI Chat Integration
Add dependencies for Camel LangChain4j and the OpenAI starter, then configure:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-langchain4j-chat-starter</artifactId>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
</dependency> langchain4j:
open-ai:
chat-model:
api-key: sk-xxxooo
model-name: qwen-turbo
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
temperature: 0.1Use requestBodyAndHeader with ChatHeaders.PROMPT_TEMPLATE to send prompts, optionally with variables.
For more Camel applications, see the linked article.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
