Skip to content

Commit 890f7fd

Browse files
authored
Flatten the menu (#194)
Signed-off-by: Andy Tael <andy.tael@yahoo.com>
1 parent cf409ac commit 890f7fd

File tree

7 files changed

+319
-305
lines changed

7 files changed

+319
-305
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright (c) 2025, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
3+
4+
[#aqjms]
5+
== AQ/JMS
6+
7+
This starter provides support for Oracle Transactional Event Queues (TxEventQ) and Oracle Advanced Queuing (AQ) as Java Message Service (JMS) providers. It depends on the Universal Connection Pool (UCP) starter.
8+
9+
**Note**: By default, the data Source and JMS Connection Factory that the starter injects into your application share the same database transaction. This means that you can start a transaction, read from a queue, perform an update operation, and then commit or rollback that whole unit of work, including the message consumption.
10+
11+
To add this starter to your project, add this Maven dependency:
12+
13+
[source,xml]
14+
----
15+
<dependency>
16+
<groupId>com.oracle.database.spring</groupId>
17+
<artifactId>oracle-spring-boot-starter-aqjms</artifactId>
18+
<version>25.1.0</version>
19+
</dependency>
20+
----
21+
22+
For Gradle projects, add this dependency:
23+
24+
[source,subs="normal"]
25+
----
26+
dependencies {
27+
implementation 'com.oracle.database.spring:oracle-spring-boot-starter-aqjms:25.1.0'
28+
}
29+
----
30+
31+
=== Using AQ/JMS
32+
33+
To configure your application to use Oracle Transactional Event Queues or Oracle Advanced Queuing, you must annotate you application with the `@EnableJms` annotation, and create the
34+
two following beans:
35+
36+
* A `JmsListenerContainerFactory<?>` bean, which can be created as shown in the following example. Note that you can override settings if you need to. Also, note that the name of the method defines the name of the factory, which you will use when creating JMS listeners.
37+
* A `MessageConverter` bean to map objects of your class representing the payload into a text based format (like JSON) that can be used in the actual messages.
38+
39+
**Note**: Any queues or topics that you want to use must be pre-created in the database. See [Sample Code](https://www.oracle.com/database/advanced-queuing/#rc30sample-code) for
40+
examples.
41+
42+
[source,java]
43+
----
44+
package com.example.aqjms;
45+
46+
import jakarta.jms.ConnectionFactory;
47+
48+
import org.springframework.boot.SpringApplication;
49+
import org.springframework.boot.autoconfigure.SpringBootApplication;
50+
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
51+
import org.springframework.context.ConfigurableApplicationContext;
52+
import org.springframework.context.annotation.Bean;
53+
import org.springframework.jms.annotation.EnableJms;
54+
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
55+
import org.springframework.jms.config.JmsListenerContainerFactory;
56+
import org.springframework.jms.core.JmsTemplate;
57+
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
58+
import org.springframework.jms.support.converter.MessageConverter;
59+
import org.springframework.jms.support.converter.MessageType;
60+
61+
@SpringBootApplication
62+
@EnableJms
63+
public class JmsSampleApplication {
64+
65+
@Bean
66+
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
67+
DefaultJmsListenerContainerFactoryConfigurer configurer) {
68+
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
69+
// This provides all Boot's defaults to this factory, including the message converter
70+
configurer.configure(factory, connectionFactory);
71+
// You could override some of Boot's defaults here if necessary
72+
return factory;
73+
}
74+
75+
@Bean
76+
public MessageConverter jacksonJmsMessageConverter() {
77+
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
78+
converter.setTargetType(MessageType.TEXT);
79+
converter.setTypeIdPropertyName("_type");
80+
return converter;
81+
}
82+
83+
public static void main(String[] args) {
84+
ConfigurableApplicationContext context = SpringApplication.run(JmsSampleApplication.class, args);
85+
}
86+
87+
}
88+
----
89+
90+
To send a message to a JMS queue or topic, get an instance of the `JmsTemplate` from the Spring Application context, and call the `convertAndSend()` method specifying the name of the queue or
91+
topic, and providing the object to be converted and sent in the payload of the message, as shown in the following example:
92+
93+
[source,java]
94+
----
95+
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
96+
jmsTemplate.convertAndSend("mailbox", new Email(-1, "info@example.com", "Hello"));
97+
----
98+
99+
To receive messages from a JMS queue or topic, create a method that takes your message class, for example `Email`, as input. Annotate the method with the `@JmsListener` annotation, specifying the destination, that is the name of the queue or topic, and the container factory name that you created earlier, as shown in the following example:
100+
101+
[source,java]
102+
----
103+
package com.example.aqjms;
104+
105+
import org.springframework.jms.annotation.JmsListener;
106+
import org.springframework.stereotype.Component;
107+
108+
@Component
109+
public class Receiver {
110+
111+
@JmsListener(destination = "mailbox", containerFactory = "myFactory")
112+
public void receiveMessage(Email email) {
113+
System.out.println("Received <" + email + ">");
114+
}
115+
116+
}
117+
----
118+
119+
**Note**: The starter uses the configuration for `spring.datasource` as the connection details for the Oracle Database hosting the queues and topics. If you wish to use a different configuration, you must use a named configuration, for example `spring.datasource.txeventq` and use Java configuration and annotate the configuration with the standard Spring `@Qualifier` annotation, specifying the correct name, for example `txevevntq`.
120+

0 commit comments

Comments
 (0)