Java Kafka Event Streaming
Implement event-driven architecture with Kafka producers, consumers, and stream processing.
Overview
Implement event-driven architecture with Kafka producers, consumers, and stream processing.
Project Structure
src/
├── main/
│ ├── java/com/example/
│ │ ├── controller/
│ │ ├── service/
│ │ ├── repository/
│ │ ├── model/
│ │ └── config/
│ └── resources/
│ └── application.yml
└── test/Dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>Model
@Entity
@Table(name = "resources")
public class Resource {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Name is required")
@Size(max = 255)
private String name;
@Column(columnDefinition = "TEXT")
private String description;
@Enumerated(EnumType.STRING)
private Status status = Status.ACTIVE;
@CreationTimestamp
private LocalDateTime createdAt;
// Getters, setters, constructors
}Service Layer
@Service
@Transactional
public class ResourceService {
private final ResourceRepository repository;
public ResourceService(ResourceRepository repository) {
this.repository = repository;
}
public List<Resource> findAll() {
return repository.findAll(Sort.by(Sort.Direction.DESC, "createdAt"));
}
public Resource findById(Long id) {
return repository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Resource not found: " + id));
}
public Resource create(ResourceRequest request) {
Resource resource = new Resource();
resource.setName(request.getName());
resource.setDescription(request.getDescription());
return repository.save(resource);
}
}REST Controller
@RestController
@RequestMapping("/api/resources")
public class ResourceController {
private final ResourceService service;
@GetMapping
public ResponseEntity<List<Resource>> getAll() {
return ResponseEntity.ok(service.findAll());
}
@PostMapping
public ResponseEntity<Resource> create(@Valid @RequestBody ResourceRequest request) {
Resource created = service.create(request);
return ResponseEntity.status(HttpStatus.CREATED).body(created);
}
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ErrorResponse(ex.getMessage()));
}
}Key Takeaways
- Follows layered architecture pattern for clean separation of concerns
- Uses Spring validation annotations for input checking
- Exception handling with \@ExceptionHandler\ for consistent error responses
- Java, Kafka, Event Streaming, Messaging integration demonstrated
Related Projects
Java Batch Processing with Spring
Process large datasets with Spring Batch job scheduling, chunking, and retry logic.
Java Socket Programming
Build networked applications with TCP/UDP sockets, serialization, and protocol design.
Java Caching with Redis
Implement distributed caching using Spring Data Redis with TTL, eviction, and pub/sub.
Comments (0)
No comments yet. Be the first to comment!