diff --git a/framework-docs/framework-docs.gradle b/framework-docs/framework-docs.gradle index f6f93ee6b9..0f9ae19063 100644 --- a/framework-docs/framework-docs.gradle +++ b/framework-docs/framework-docs.gradle @@ -79,6 +79,9 @@ dependencies { api("org.aspectj:aspectjweaver") api("io.projectreactor.netty:reactor-netty-http") api("org.eclipse.jetty.websocket:jetty-websocket-jetty-api") + api("javax.cache:cache-api") + api("jakarta.resource:jakarta.resource-api") + api("org.apache.activemq:activemq-ra:6.1.2") implementation(project(":spring-core-test")) implementation("org.assertj:assertj-core") diff --git a/framework-docs/modules/ROOT/pages/integration/cache/annotations.adoc b/framework-docs/modules/ROOT/pages/integration/cache/annotations.adoc index a455ac6ef2..0398dbd90a 100644 --- a/framework-docs/modules/ROOT/pages/integration/cache/annotations.adoc +++ b/framework-docs/modules/ROOT/pages/integration/cache/annotations.adoc @@ -518,41 +518,9 @@ disable it by removing only one configuration line rather than all the annotatio your code). To enable caching annotations add the annotation `@EnableCaching` to one of your -`@Configuration` classes: +`@Configuration` classes or use the `cache:annotation-driven` element with XML: -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableCaching - public class AppConfig { - - @Bean - CacheManager cacheManager() { - CaffeineCacheManager cacheManager = new CaffeineCacheManager(); - cacheManager.setCacheSpecification(...); - return cacheManager; - } - } ----- - -Alternatively, for XML configuration you can use the `cache:annotation-driven` element: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - ----- +include-code::./CacheConfiguration[tag=snippet,indent=0] Both the `cache:annotation-driven` element and the `@EnableCaching` annotation let you specify various options that influence the way the caching behavior is added to the diff --git a/framework-docs/modules/ROOT/pages/integration/cache/store-configuration.adoc b/framework-docs/modules/ROOT/pages/integration/cache/store-configuration.adoc index c8009ce251..ed350b2385 100644 --- a/framework-docs/modules/ROOT/pages/integration/cache/store-configuration.adoc +++ b/framework-docs/modules/ROOT/pages/integration/cache/store-configuration.adoc @@ -13,18 +13,7 @@ The JDK-based `Cache` implementation resides under `org.springframework.cache.concurrent` package. It lets you use `ConcurrentHashMap` as a backing `Cache` store. The following example shows how to configure two caches: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - ----- +include-code::./CacheConfiguration[tag=snippet,indent=0] The preceding snippet uses the `SimpleCacheManager` to create a `CacheManager` for the two nested `ConcurrentMapCache` instances named `default` and `books`. Note that the @@ -52,26 +41,12 @@ of Caffeine. The following example configures a `CacheManager` that creates the cache on demand: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - ----- +include-code::./CacheConfiguration[tag=snippet,indent=0] You can also provide the caches to use explicitly. In that case, only those are made available by the manager. The following example shows how to do so: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - default - books - - - ----- +include-code::./CustomCacheConfiguration[tag=snippet,indent=0] The Caffeine `CacheManager` also supports custom `Caffeine` and `CacheLoader`. See the https://github.com/ben-manes/caffeine/wiki[Caffeine documentation] @@ -97,15 +72,7 @@ implementation is located in the `org.springframework.cache.jcache` package. Again, to use it, you need to declare the appropriate `CacheManager`. The following example shows how to do so: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - ----- +include-code::./CacheConfiguration[tag=snippet,indent=0] [[cache-store-configuration-noop]] @@ -119,18 +86,7 @@ cache declarations (which can prove tedious), you can wire in a simple dummy cac performs no caching -- that is, it forces the cached methods to be invoked every time. The following example shows how to do so: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - ----- +include-code::./CacheConfiguration[tag=snippet,indent=0] The `CompositeCacheManager` in the preceding chains multiple `CacheManager` instances and, through the `fallbackToNoOpCache` flag, adds a no-op cache for all the definitions not diff --git a/framework-docs/modules/ROOT/pages/integration/email.adoc b/framework-docs/modules/ROOT/pages/integration/email.adoc index 19568c212c..46493a7de9 100644 --- a/framework-docs/modules/ROOT/pages/integration/email.adoc +++ b/framework-docs/modules/ROOT/pages/integration/email.adoc @@ -41,14 +41,7 @@ JavaMail features, such as MIME message support to the `MailSender` interface Assume that we have a business interface called `OrderManager`, as the following example shows: -[source,java,indent=0,subs="verbatim,quotes"] ----- - public interface OrderManager { - - void placeOrder(Order order); - - } ----- +include-code::./OrderManager[tag=snippet,indent=0] Further assume that we have a requirement stating that an email message with an order number needs to be generated and sent to a customer who placed the relevant order. @@ -60,70 +53,11 @@ order number needs to be generated and sent to a customer who placed the relevan The following example shows how to use `MailSender` and `SimpleMailMessage` to send an email when someone places an order: -[source,java,indent=0,subs="verbatim,quotes"] ----- - import org.springframework.mail.MailException; - import org.springframework.mail.MailSender; - import org.springframework.mail.SimpleMailMessage; - - public class SimpleOrderManager implements OrderManager { - - private MailSender mailSender; - private SimpleMailMessage templateMessage; - - public void setMailSender(MailSender mailSender) { - this.mailSender = mailSender; - } - - public void setTemplateMessage(SimpleMailMessage templateMessage) { - this.templateMessage = templateMessage; - } - - public void placeOrder(Order order) { - - // Do the business calculations... - - // Call the collaborators to persist the order... - - // Create a thread-safe "copy" of the template message and customize it - SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage); - msg.setTo(order.getCustomer().getEmailAddress()); - msg.setText( - "Dear " + order.getCustomer().getFirstName() - + order.getCustomer().getLastName() - + ", thank you for placing order. Your order number is " - + order.getOrderNumber()); - try { - this.mailSender.send(msg); - } - catch (MailException ex) { - // simply log it and go on... - System.err.println(ex.getMessage()); - } - } - - } ----- +include-code::./SimpleOrderManager[tag=snippet,indent=0] The following example shows the bean definitions for the preceding code: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - - - - - - ----- +include-code::./MailConfiguration[tag=snippet,indent=0] [[mail-usage-mime]] diff --git a/framework-docs/modules/ROOT/pages/integration/jms/annotated.adoc b/framework-docs/modules/ROOT/pages/integration/jms/annotated.adoc index a0ffb80fb0..db67901d7c 100644 --- a/framework-docs/modules/ROOT/pages/integration/jms/annotated.adoc +++ b/framework-docs/modules/ROOT/pages/integration/jms/annotated.adoc @@ -37,23 +37,7 @@ declarations to it. To enable support for `@JmsListener` annotations, you can add `@EnableJms` to one of your `@Configuration` classes, as the following example shows: -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableJms - public class AppConfig { - - @Bean - public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() { - DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); - factory.setConnectionFactory(connectionFactory()); - factory.setDestinationResolver(destinationResolver()); - factory.setSessionTransacted(true); - factory.setConcurrency("3-10"); - return factory; - } - } ----- +include-code::./JmsConfiguration[tag=snippet,indent=0] By default, the infrastructure looks for a bean named `jmsListenerContainerFactory` as the source for the factory to use to create message listener containers. In this @@ -67,22 +51,6 @@ container factory. See the javadoc of classes that implement {spring-framework-api}/jms/annotation/JmsListenerConfigurer.html[`JmsListenerConfigurer`] for details and examples. -If you prefer xref:integration/jms/namespace.adoc[XML configuration], you can use the `` -element, as the following example shows: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - ----- - [[jms-annotated-programmatic-registration]] == Programmatic Endpoint Registration diff --git a/framework-docs/modules/ROOT/pages/integration/jms/jca-message-endpoint-manager.adoc b/framework-docs/modules/ROOT/pages/integration/jms/jca-message-endpoint-manager.adoc index 70942ae58d..8838f449d7 100644 --- a/framework-docs/modules/ROOT/pages/integration/jms/jca-message-endpoint-manager.adoc +++ b/framework-docs/modules/ROOT/pages/integration/jms/jca-message-endpoint-manager.adoc @@ -7,62 +7,13 @@ automatically determine the `ActivationSpec` class name from the provider's `ResourceAdapter` class name. Therefore, it is typically possible to provide Spring's generic `JmsActivationSpecConfig`, as the following example shows: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - ----- +include-code::./JmsConfiguration[tag=snippet,indent=0] Alternatively, you can set up a `JmsMessageEndpointManager` with a given `ActivationSpec` object. The `ActivationSpec` object may also come from a JNDI lookup (using ``). The following example shows how to do so: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - - ----- - -Using Spring's `ResourceAdapterFactoryBean`, you can configure the target `ResourceAdapter` -locally, as the following example shows: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - - ----- - -The specified `WorkManager` can also point to an environment-specific thread pool -- -typically through a `SimpleTaskWorkManager` instance's `asyncTaskExecutor` property. -Consider defining a shared thread pool for all your `ResourceAdapter` instances -if you happen to use multiple adapters. - -In some environments, you can instead obtain the entire `ResourceAdapter` object from JNDI -(by using ``). The Spring-based message listeners can then interact with -the server-hosted `ResourceAdapter`, which also use the server's built-in `WorkManager`. +include-code::./AlternativeJmsConfiguration[tag=snippet,indent=0] See the javadoc for {spring-framework-api}/jms/listener/endpoint/JmsMessageEndpointManager.html[`JmsMessageEndpointManager`], {spring-framework-api}/jms/listener/endpoint/JmsActivationSpecConfig.html[`JmsActivationSpecConfig`], diff --git a/framework-docs/modules/ROOT/pages/integration/jms/receiving.adoc b/framework-docs/modules/ROOT/pages/integration/jms/receiving.adoc index 81388f3ae0..f56dae3e3f 100644 --- a/framework-docs/modules/ROOT/pages/integration/jms/receiving.adoc +++ b/framework-docs/modules/ROOT/pages/integration/jms/receiving.adoc @@ -31,30 +31,7 @@ on multiple threads, it is important to ensure that your implementation is threa The following example shows a simple implementation of an MDP: -[source,java,indent=0,subs="verbatim,quotes"] ----- - import jakarta.jms.JMSException; - import jakarta.jms.Message; - import jakarta.jms.MessageListener; - import jakarta.jms.TextMessage; - - public class ExampleListener implements MessageListener { - - public void onMessage(Message message) { - if (message instanceof TextMessage textMessage) { - try { - System.out.println(textMessage.getText()); - } - catch (JMSException ex) { - throw new RuntimeException(ex); - } - } - else { - throw new IllegalArgumentException("Message must be of type TextMessage"); - } - } - } ----- +include-code::./ExampleListener[tag=snippet,indent=0] Once you have implemented your `MessageListener`, it is time to create a message listener container. @@ -62,18 +39,7 @@ container. The following example shows how to define and configure one of the message listener containers that ships with Spring (in this case, `DefaultMessageListenerContainer`): -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - ----- +include-code::./JmsConfiguration[tag=snippet,indent=0] See the Spring javadoc of the various message listener containers (all of which implement {spring-framework-api}/jms/listener/MessageListenerContainer.html[MessageListenerContainer]) @@ -123,19 +89,7 @@ messaging support. In a nutshell, it lets you expose almost any class as an MDP Consider the following interface definition: -[source,java,indent=0,subs="verbatim,quotes"] ----- - public interface MessageDelegate { - - void handleMessage(String message); - - void handleMessage(Map message); - - void handleMessage(byte[] message); - - void handleMessage(Serializable message); - } ----- +include-code::./MessageDelegate[tag=snippet,indent=0] Notice that, although the interface extends neither the `MessageListener` nor the `SessionAwareMessageListener` interface, you can still use it as an MDP by using the @@ -145,33 +99,13 @@ receive and handle. Now consider the following implementation of the `MessageDelegate` interface: -[source,java,indent=0,subs="verbatim,quotes"] ----- - public class DefaultMessageDelegate implements MessageDelegate { - // implementation elided for clarity... - } ----- +include-code::./DefaultMessageDelegate[tag=snippet,indent=0] In particular, note how the preceding implementation of the `MessageDelegate` interface (the `DefaultMessageDelegate` class) has no JMS dependencies at all. It truly is a POJO that we can make into an MDP through the following configuration: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - - - - - ----- +include-code::./JmsConfiguration[tag=snippet,indent=0] The next example shows another MDP that can handle only receiving JMS `TextMessage` messages. Notice how the message handling method is actually called @@ -181,38 +115,15 @@ also how the `receive(..)` method is strongly typed to receive and respond only `TextMessage` messages. The following listing shows the definition of the `TextMessageDelegate` interface: -[source,java,indent=0,subs="verbatim,quotes"] ----- - public interface TextMessageDelegate { - - void receive(TextMessage message); - } ----- +include-code::./TextMessageDelegate[tag=snippet,indent=0] The following listing shows a class that implements the `TextMessageDelegate` interface: -[source,java,indent=0,subs="verbatim,quotes"] ----- - public class DefaultTextMessageDelegate implements TextMessageDelegate { - // implementation elided for clarity... - } ----- +include-code::./DefaultTextMessageDelegate[tag=snippet,indent=0] The configuration of the attendant `MessageListenerAdapter` would then be as follows: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - - ----- +include-code::./MessageListenerConfiguration[tag=snippet,indent=0] Note that, if the `messageListener` receives a JMS `Message` of a type other than `TextMessage`, an `IllegalStateException` is thrown (and subsequently @@ -220,21 +131,9 @@ swallowed). Another of the capabilities of the `MessageListenerAdapter` class is ability to automatically send back a response `Message` if a handler method returns a non-void value. Consider the following interface and class: -[source,java,indent=0,subs="verbatim,quotes"] ----- - public interface ResponsiveTextMessageDelegate { +include-code::./ResponsiveTextMessageDelegate[tag=snippet,indent=0] - // notice the return type... - String receive(TextMessage message); - } ----- - -[source,java,indent=0,subs="verbatim,quotes"] ----- - public class DefaultResponsiveTextMessageDelegate implements ResponsiveTextMessageDelegate { - // implementation elided for clarity... - } ----- +include-code::./DefaultResponsiveTextMessageDelegate[tag=snippet,indent=0] If you use the `DefaultResponsiveTextMessageDelegate` in conjunction with a `MessageListenerAdapter`, any non-null value that is returned from the execution of @@ -264,15 +163,7 @@ has committed but message processing failed to commit. Consider the following bean definition: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - ----- +include-code::./JmsConfiguration[tag=snippet,indent=0] To participate in an externally managed transaction, you need to configure a transaction manager and use a listener container that supports externally managed @@ -288,24 +179,9 @@ semantics, at the expense of XA transaction log overhead). The following bean definition creates a transaction manager: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - ----- +include-code::./ExternalTxJmsConfiguration[tag=transactionManagerSnippet,indent=0] Then we need to add it to our earlier container configuration. The container takes care of the rest. The following example shows how to do so: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - <1> - ----- -<1> Our transaction manager. - - - +include-code::./ExternalTxJmsConfiguration[tag=jmsContainerSnippet,indent=0] diff --git a/framework-docs/modules/ROOT/pages/integration/jmx/exporting.adoc b/framework-docs/modules/ROOT/pages/integration/jmx/exporting.adoc index 2b1fb86637..138c867a20 100644 --- a/framework-docs/modules/ROOT/pages/integration/jmx/exporting.adoc +++ b/framework-docs/modules/ROOT/pages/integration/jmx/exporting.adoc @@ -5,63 +5,13 @@ The core class in Spring's JMX framework is the `MBeanExporter`. This class is responsible for taking your Spring beans and registering them with a JMX `MBeanServer`. For example, consider the following class: -[source,java,indent=0,subs="verbatim,quotes",chomp="-packages",chomp="-packages"] ----- - package org.springframework.jmx; - - public class JmxTestBean implements IJmxTestBean { - - private String name; - private int age; - private boolean isSuperman; - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public int add(int x, int y) { - return x + y; - } - - public void dontExposeMe() { - throw new RuntimeException(); - } - } ----- +include-code::./JmxTestBean[tag=snippet,indent=0] To expose the properties and methods of this bean as attributes and operations of an MBean, you can configure an instance of the `MBeanExporter` class in your configuration file and pass in the bean, as the following example shows: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - - - - - - ----- +include-code::./JmxConfiguration[tag=snippet,indent=0] The pertinent bean definition from the preceding configuration snippet is the `exporter` bean. The `beans` property tells the `MBeanExporter` exactly which of your beans must be diff --git a/framework-docs/modules/ROOT/pages/integration/jmx/naming.adoc b/framework-docs/modules/ROOT/pages/integration/jmx/naming.adoc index 39b9582819..cdaf804322 100644 --- a/framework-docs/modules/ROOT/pages/integration/jmx/naming.adoc +++ b/framework-docs/modules/ROOT/pages/integration/jmx/naming.adoc @@ -122,25 +122,10 @@ your management interfaces, a convenience subclass of `MBeanExporter` is availab `namingStrategy`, `assembler`, and `attributeSource` configuration, since it always uses standard Java annotation-based metadata (autodetection is always enabled as well). In fact, rather than defining an `MBeanExporter` bean, an even -simpler syntax is supported by the `@EnableMBeanExport` `@Configuration` annotation, -as the following example shows: +simpler syntax is supported by the `@EnableMBeanExport` `@Configuration` annotation or the `` +element as the following example shows: -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableMBeanExport - public class AppConfig { - - } ----- - -If you prefer XML-based configuration, the `` element serves the -same purpose and is shown in the following listing: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - ----- +include-code::./JmxConfiguration[tag=snippet,indent=0] If necessary, you can provide a reference to a particular MBean `server`, and the `defaultDomain` attribute (a property of `AnnotationMBeanExporter`) accepts an alternate @@ -148,21 +133,7 @@ value for the generated MBean `ObjectName` domains. This is used in place of the fully qualified package name as described in the previous section on xref:integration/jmx/naming.adoc#jmx-naming-metadata[MetadataNamingStrategy], as the following example shows: -[source,java,indent=0,subs="verbatim,quotes"] ----- - @EnableMBeanExport(server="myMBeanServer", defaultDomain="myDomain") - @Configuration - ContextConfiguration { - - } ----- - -The following example shows the XML equivalent of the preceding annotation-based example: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - ----- +include-code::./CustomJmxConfiguration[tag=snippet,indent=0] CAUTION: Do not use interface-based AOP proxies in combination with autodetection of JMX annotations in your bean classes. Interface-based proxies "`hide`" the target class, which diff --git a/framework-docs/modules/ROOT/pages/integration/scheduling.adoc b/framework-docs/modules/ROOT/pages/integration/scheduling.adoc index 796d1ea326..e78997fa4a 100644 --- a/framework-docs/modules/ROOT/pages/integration/scheduling.adoc +++ b/framework-docs/modules/ROOT/pages/integration/scheduling.adoc @@ -79,38 +79,7 @@ Spring's `TaskExecutor` implementations are commonly used with dependency inject In the following example, we define a bean that uses the `ThreadPoolTaskExecutor` to asynchronously print out a set of messages: -[source,java,indent=0,subs="verbatim,quotes"] ----- - import org.springframework.core.task.TaskExecutor; - - public class TaskExecutorExample { - - private class MessagePrinterTask implements Runnable { - - private String message; - - public MessagePrinterTask(String message) { - this.message = message; - } - - public void run() { - System.out.println(message); - } - } - - private TaskExecutor taskExecutor; - - public TaskExecutorExample(TaskExecutor taskExecutor) { - this.taskExecutor = taskExecutor; - } - - public void printMessages() { - for(int i = 0; i < 25; i++) { - taskExecutor.execute(new MessagePrinterTask("Message" + i)); - } - } - } ----- +include-code::./TaskExecutorExample[tag=snippet,indent=0] As you can see, rather than retrieving a thread from the pool and executing it yourself, you add your `Runnable` to the queue. Then the `TaskExecutor` uses its internal rules to @@ -118,19 +87,7 @@ decide when the task gets run. To configure the rules that the `TaskExecutor` uses, we expose simple bean properties: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - ----- - +include-code::./TaskExecutorConfiguration[tag=snippet,indent=0] [[scheduling-task-scheduler]] @@ -269,16 +226,10 @@ execution. === Enable Scheduling Annotations To enable support for `@Scheduled` and `@Async` annotations, you can add `@EnableScheduling` -and `@EnableAsync` to one of your `@Configuration` classes, as the following example shows: +and `@EnableAsync` to one of your `@Configuration` classes, or `` element, +as the following example shows: -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableAsync - @EnableScheduling - public class AppConfig { - } ----- +include-code::./SchedulingConfiguration[tag=snippet,indent=0] You can pick and choose the relevant annotations for your application. For example, if you need only support for `@Scheduled`, you can omit `@EnableAsync`. For more @@ -288,16 +239,6 @@ interface, the `AsyncConfigurer` interface, or both. See the and {spring-framework-api}/scheduling/annotation/AsyncConfigurer.html[`AsyncConfigurer`] javadoc for full details. -If you prefer XML configuration, you can use the `` element, -as the following example shows: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - ----- - Note that, with the preceding XML, an executor reference is provided for handling those tasks that correspond to methods with the `@Async` annotation, and the scheduler reference is provided for managing those methods annotated with `@Scheduled`. diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/cache/cacheannotationenable/CacheConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cacheannotationenable/CacheConfiguration.java new file mode 100644 index 0000000000..53411fa713 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cacheannotationenable/CacheConfiguration.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cacheannotationenable; + +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +// tag::snippet[] +@Configuration +@EnableCaching +public class CacheConfiguration { + + @Bean + CacheManager cacheManager() { + CaffeineCacheManager cacheManager = new CaffeineCacheManager(); + cacheManager.setCacheSpecification("..."); + return cacheManager; + } +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CacheConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CacheConfiguration.java new file mode 100644 index 0000000000..6618ee3353 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CacheConfiguration.java @@ -0,0 +1,33 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cachestoreconfigurationcaffeine; + +import org.springframework.cache.CacheManager; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CacheConfiguration { + + // tag::snippet[] + @Bean + CacheManager cacheManager() { + return new CaffeineCacheManager(); + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CustomCacheConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CustomCacheConfiguration.java new file mode 100644 index 0000000000..db87fcc981 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CustomCacheConfiguration.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cachestoreconfigurationcaffeine; + +import java.util.List; + +import org.springframework.cache.CacheManager; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CustomCacheConfiguration { + + // tag::snippet[] + @Bean + CacheManager cacheManager() { + CaffeineCacheManager cacheManager = new CaffeineCacheManager(); + cacheManager.setCacheNames(List.of("default", "books")); + return cacheManager; + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationjdk/CacheConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationjdk/CacheConfiguration.java new file mode 100644 index 0000000000..c8fae52e56 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationjdk/CacheConfiguration.java @@ -0,0 +1,54 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cachestoreconfigurationjdk; + +import java.util.Set; + +import org.springframework.cache.CacheManager; +import org.springframework.cache.concurrent.ConcurrentMapCache; +import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean; +import org.springframework.cache.support.SimpleCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CacheConfiguration { + + // tag::snippet[] + @Bean + ConcurrentMapCacheFactoryBean defaultCache() { + ConcurrentMapCacheFactoryBean cache = new ConcurrentMapCacheFactoryBean(); + cache.setName("default"); + return cache; + } + + @Bean + ConcurrentMapCacheFactoryBean booksCache() { + ConcurrentMapCacheFactoryBean cache = new ConcurrentMapCacheFactoryBean(); + cache.setName("books"); + return cache; + } + + @Bean + CacheManager cacheManager(ConcurrentMapCache defaultCache, ConcurrentMapCache booksCache) { + + SimpleCacheManager cacheManager = new SimpleCacheManager(); + cacheManager.setCaches(Set.of(defaultCache, booksCache)); + return cacheManager; + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationjsr107/CacheConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationjsr107/CacheConfiguration.java new file mode 100644 index 0000000000..1c83d6c446 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationjsr107/CacheConfiguration.java @@ -0,0 +1,41 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cachestoreconfigurationjsr107; + +import javax.cache.Caching; +import javax.cache.spi.CachingProvider; + +import org.springframework.cache.jcache.JCacheCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CacheConfiguration { + + // tag::snippet[] + @Bean + javax.cache.CacheManager jCacheManager() { + CachingProvider cachingProvider = Caching.getCachingProvider(); + return cachingProvider.getCacheManager(); + } + + @Bean + org.springframework.cache.CacheManager cacheManager(javax.cache.CacheManager jCacheManager) { + return new JCacheCacheManager(jCacheManager); + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationnoop/CacheConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationnoop/CacheConfiguration.java new file mode 100644 index 0000000000..6944e7b785 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/cache/cachestoreconfigurationnoop/CacheConfiguration.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cachestoreconfigurationnoop; + +import java.util.List; + +import org.springframework.cache.CacheManager; +import org.springframework.cache.support.CompositeCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CacheConfiguration { + + private CacheManager jdkCache() { + return null; + } + + private CacheManager gemfireCache() { + return null; + } + + // tag::snippet[] + @Bean + CacheManager cacheManager(CacheManager jdkCache, CacheManager gemfireCache) { + CompositeCacheManager cacheManager = new CompositeCacheManager(); + cacheManager.setCacheManagers(List.of(jdkCache, gemfireCache)); + cacheManager.setFallbackToNoOpCache(true); + return cacheManager; + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsannotatedsupport/JmsConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsannotatedsupport/JmsConfiguration.java new file mode 100644 index 0000000000..932988ce63 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsannotatedsupport/JmsConfiguration.java @@ -0,0 +1,45 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsannotatedsupport; + + +import jakarta.jms.ConnectionFactory; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jms.annotation.EnableJms; +import org.springframework.jms.config.DefaultJmsListenerContainerFactory; +import org.springframework.jms.support.destination.DestinationResolver; + +// tag::snippet[] +@Configuration +@EnableJms +public class JmsConfiguration { + + @Bean + public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory, + DestinationResolver destinationResolver) { + + DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); + factory.setConnectionFactory(connectionFactory); + factory.setDestinationResolver(destinationResolver); + factory.setSessionTransacted(true); + factory.setConcurrency("3-10"); + return factory; + } +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/AlternativeJmsConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/AlternativeJmsConfiguration.java new file mode 100644 index 0000000000..dc183df8eb --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/AlternativeJmsConfiguration.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsjcamessageendpointmanager; + +import jakarta.jms.MessageListener; +import jakarta.resource.spi.ResourceAdapter; +import org.apache.activemq.ra.ActiveMQActivationSpec; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jms.listener.endpoint.JmsMessageEndpointManager; + +@Configuration +public class AlternativeJmsConfiguration { + + // tag::snippet[] + @Bean + JmsMessageEndpointManager jmsMessageEndpointManager(ResourceAdapter resourceAdapter, + MessageListener myMessageListener) { + + ActiveMQActivationSpec spec = new ActiveMQActivationSpec(); + spec.setDestination("myQueue"); + spec.setDestinationType("jakarta.jms.Queue"); + + JmsMessageEndpointManager endpointManager = new JmsMessageEndpointManager(); + endpointManager.setResourceAdapter(resourceAdapter); + endpointManager.setActivationSpec(spec); + endpointManager.setMessageListener(myMessageListener); + return endpointManager; + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/JmsConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/JmsConfiguration.java new file mode 100644 index 0000000000..a95e1c8aaf --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/JmsConfiguration.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsjcamessageendpointmanager; + + +import jakarta.jms.MessageListener; +import jakarta.resource.spi.ResourceAdapter; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jms.listener.endpoint.JmsActivationSpecConfig; +import org.springframework.jms.listener.endpoint.JmsMessageEndpointManager; + +@Configuration +public class JmsConfiguration { + + // tag::snippet[] + @Bean + public JmsMessageEndpointManager jmsMessageEndpointManager(ResourceAdapter resourceAdapter, + MessageListener myMessageListener) { + + JmsActivationSpecConfig specConfig = new JmsActivationSpecConfig(); + specConfig.setDestinationName("myQueue"); + + JmsMessageEndpointManager endpointManager = new JmsMessageEndpointManager(); + endpointManager.setResourceAdapter(resourceAdapter); + endpointManager.setActivationSpecConfig(specConfig); + endpointManager.setMessageListener(myMessageListener); + return endpointManager; + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasync/ExampleListener.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasync/ExampleListener.java new file mode 100644 index 0000000000..d535f8cb08 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasync/ExampleListener.java @@ -0,0 +1,41 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasync; + +import jakarta.jms.JMSException; +import jakarta.jms.Message; +import jakarta.jms.MessageListener; +import jakarta.jms.TextMessage; + +// tag::snippet[] +public class ExampleListener implements MessageListener { + + public void onMessage(Message message) { + if (message instanceof TextMessage textMessage) { + try { + System.out.println(textMessage.getText()); + } + catch (JMSException ex) { + throw new RuntimeException(ex); + } + } + else { + throw new IllegalArgumentException("Message must be of type TextMessage"); + } + } +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasync/JmsConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasync/JmsConfiguration.java new file mode 100644 index 0000000000..6f9fb309ae --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasync/JmsConfiguration.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasync; + +import jakarta.jms.ConnectionFactory; +import jakarta.jms.Destination; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jms.listener.DefaultMessageListenerContainer; + +@Configuration +public class JmsConfiguration { + + // tag::snippet[] + @Bean + ExampleListener messageListener() { + return new ExampleListener(); + } + + @Bean + DefaultMessageListenerContainer jmsContainer(ConnectionFactory connectionFactory, Destination destination, + ExampleListener messageListener) { + + DefaultMessageListenerContainer jmsContainer = new DefaultMessageListenerContainer(); + jmsContainer.setConnectionFactory(connectionFactory); + jmsContainer.setDestination(destination); + jmsContainer.setMessageListener(messageListener); + return jmsContainer; + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultMessageDelegate.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultMessageDelegate.java new file mode 100644 index 0000000000..acad7cbcc2 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultMessageDelegate.java @@ -0,0 +1,45 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter; + +import java.io.Serializable; +import java.util.Map; + +// tag::snippet[] +public class DefaultMessageDelegate implements MessageDelegate { + + @Override + public void handleMessage(String message) { + // ... + } + + @Override + public void handleMessage(Map message) { + // ... + } + + @Override + public void handleMessage(byte[] message) { + // ... + } + + @Override + public void handleMessage(Serializable message) { + // ... + } +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultResponsiveTextMessageDelegate.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultResponsiveTextMessageDelegate.java new file mode 100644 index 0000000000..bfa2e61684 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultResponsiveTextMessageDelegate.java @@ -0,0 +1,29 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter; + +import jakarta.jms.TextMessage; + +// tag::snippet[] +public class DefaultResponsiveTextMessageDelegate implements ResponsiveTextMessageDelegate { + + @Override + public String receive(TextMessage message) { + return "message"; + } +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultTextMessageDelegate.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultTextMessageDelegate.java new file mode 100644 index 0000000000..00d32876ac --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultTextMessageDelegate.java @@ -0,0 +1,29 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter; + +import org.springframework.web.socket.TextMessage; + +// tag::snippet[] +public class DefaultTextMessageDelegate implements TextMessageDelegate { + + @Override + public void receive(TextMessage message) { + // ... + } +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/JmsConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/JmsConfiguration.java new file mode 100644 index 0000000000..6bb0bf75cd --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/JmsConfiguration.java @@ -0,0 +1,48 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter; + +import jakarta.jms.ConnectionFactory; +import jakarta.jms.Destination; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.docs.integration.jms.jmsreceivingasync.ExampleListener; +import org.springframework.jms.listener.DefaultMessageListenerContainer; +import org.springframework.jms.listener.adapter.MessageListenerAdapter; + +@Configuration +public class JmsConfiguration { + + // tag::snippet[] + @Bean + MessageListenerAdapter messageListener(DefaultMessageDelegate messageDelegate) { + return new MessageListenerAdapter(messageDelegate); + } + + @Bean + DefaultMessageListenerContainer jmsContainer(ConnectionFactory connectionFactory, Destination destination, + ExampleListener messageListener) { + + DefaultMessageListenerContainer jmsContainer = new DefaultMessageListenerContainer(); + jmsContainer.setConnectionFactory(connectionFactory); + jmsContainer.setDestination(destination); + jmsContainer.setMessageListener(messageListener); + return jmsContainer; + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageDelegate.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageDelegate.java new file mode 100644 index 0000000000..f15b002ff2 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageDelegate.java @@ -0,0 +1,33 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter; + +import java.io.Serializable; +import java.util.Map; + +// tag::snippet[] +public interface MessageDelegate { + + void handleMessage(String message); + + void handleMessage(Map message); + + void handleMessage(byte[] message); + + void handleMessage(Serializable message); +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageListenerConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageListenerConfiguration.java new file mode 100644 index 0000000000..565e2dffe1 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageListenerConfiguration.java @@ -0,0 +1,36 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jms.listener.adapter.MessageListenerAdapter; + +@Configuration +public class MessageListenerConfiguration { + + // tag::snippet[] + @Bean + MessageListenerAdapter messageListener(DefaultTextMessageDelegate messageDelegate) { + MessageListenerAdapter messageListener = new MessageListenerAdapter(messageDelegate); + messageListener.setDefaultListenerMethod("receive"); + // We don't want automatic message context extraction + messageListener.setMessageConverter(null); + return messageListener; + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/ResponsiveTextMessageDelegate.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/ResponsiveTextMessageDelegate.java new file mode 100644 index 0000000000..ae22b4838f --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/ResponsiveTextMessageDelegate.java @@ -0,0 +1,27 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter; + +import jakarta.jms.TextMessage; + +// tag::snippet[] +public interface ResponsiveTextMessageDelegate { + + // Notice the return type... + String receive(TextMessage message); +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/TextMessageDelegate.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/TextMessageDelegate.java new file mode 100644 index 0000000000..ecd0f3007d --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/TextMessageDelegate.java @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter; + +import org.springframework.web.socket.TextMessage; + +// tag::snippet[] +public interface TextMessageDelegate { + + void receive(TextMessage message); +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmstxparticipation/ExternalTxJmsConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmstxparticipation/ExternalTxJmsConfiguration.java new file mode 100644 index 0000000000..cd10a82ac9 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmstxparticipation/ExternalTxJmsConfiguration.java @@ -0,0 +1,51 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmstxparticipation; + +import jakarta.jms.ConnectionFactory; +import jakarta.jms.Destination; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.docs.integration.jms.jmsreceivingasync.ExampleListener; +import org.springframework.jms.listener.DefaultMessageListenerContainer; +import org.springframework.transaction.jta.JtaTransactionManager; + +@Configuration +public class ExternalTxJmsConfiguration { + + // tag::transactionManagerSnippet[] + @Bean + JtaTransactionManager transactionManager() { + return new JtaTransactionManager(); + } + // end::transactionManagerSnippet[] + + // tag::jmsContainerSnippet[] + @Bean + DefaultMessageListenerContainer jmsContainer(ConnectionFactory connectionFactory, Destination destination, + ExampleListener messageListener) { + + DefaultMessageListenerContainer jmsContainer = new DefaultMessageListenerContainer(); + jmsContainer.setConnectionFactory(connectionFactory); + jmsContainer.setDestination(destination); + jmsContainer.setMessageListener(messageListener); + jmsContainer.setSessionTransacted(true); + return jmsContainer; + } + // end::jmsContainerSnippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmstxparticipation/JmsConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmstxparticipation/JmsConfiguration.java new file mode 100644 index 0000000000..bdc8c847e2 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jms/jmstxparticipation/JmsConfiguration.java @@ -0,0 +1,43 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmstxparticipation; + +import jakarta.jms.ConnectionFactory; +import jakarta.jms.Destination; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.docs.integration.jms.jmsreceivingasync.ExampleListener; +import org.springframework.jms.listener.DefaultMessageListenerContainer; + +@Configuration +public class JmsConfiguration { + + // tag::snippet[] + @Bean + DefaultMessageListenerContainer jmsContainer(ConnectionFactory connectionFactory, Destination destination, + ExampleListener messageListener) { + + DefaultMessageListenerContainer jmsContainer = new DefaultMessageListenerContainer(); + jmsContainer.setConnectionFactory(connectionFactory); + jmsContainer.setDestination(destination); + jmsContainer.setMessageListener(messageListener); + jmsContainer.setSessionTransacted(true); + return jmsContainer; + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/CustomJmxConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/CustomJmxConfiguration.java new file mode 100644 index 0000000000..d227fba9db --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/CustomJmxConfiguration.java @@ -0,0 +1,27 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jmx.jmxcontextmbeanexport; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableMBeanExport; + +// tag::snippet[] +@Configuration +@EnableMBeanExport(server="myMBeanServer", defaultDomain="myDomain") +public class CustomJmxConfiguration { +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/JmxConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/JmxConfiguration.java new file mode 100644 index 0000000000..70f3a08667 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/JmxConfiguration.java @@ -0,0 +1,27 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jmx.jmxcontextmbeanexport; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableMBeanExport; + +// tag::snippet[] +@Configuration +@EnableMBeanExport +public class JmxConfiguration { +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxexporting/IJmxTestBean.java b/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxexporting/IJmxTestBean.java new file mode 100644 index 0000000000..4100da2d92 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxexporting/IJmxTestBean.java @@ -0,0 +1,32 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jmx.jmxexporting; + +public interface IJmxTestBean { + + int getAge(); + + void setAge(int age); + + void setName(String name); + + String getName(); + + int add(int x, int y); + + void dontExposeMe(); +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxexporting/JmxConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxexporting/JmxConfiguration.java new file mode 100644 index 0000000000..98ce1302a6 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxexporting/JmxConfiguration.java @@ -0,0 +1,44 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jmx.jmxexporting; + +import java.util.Map; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jmx.export.MBeanExporter; + +// tag::snippet[] +@Configuration +public class JmxConfiguration { + + @Bean + MBeanExporter exporter(JmxTestBean testBean) { + MBeanExporter exporter = new MBeanExporter(); + exporter.setBeans(Map.of("bean:name=testBean1", testBean)); + return exporter; + } + + @Bean + JmxTestBean testBean() { + JmxTestBean testBean = new JmxTestBean(); + testBean.setName("TEST"); + testBean.setAge(100); + return testBean; + } +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxexporting/JmxTestBean.java b/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxexporting/JmxTestBean.java new file mode 100644 index 0000000000..6412ecabf9 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/jmx/jmxexporting/JmxTestBean.java @@ -0,0 +1,55 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jmx.jmxexporting; + +// tag::snippet[] +public class JmxTestBean implements IJmxTestBean { + + private String name; + private int age; + + @Override + public int getAge() { + return age; + } + + @Override + public void setAge(int age) { + this.age = age; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public String getName() { + return name; + } + + @Override + public int add(int x, int y) { + return x + y; + } + + @Override + public void dontExposeMe() { + throw new RuntimeException(); + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/mailusage/OrderManager.java b/framework-docs/src/main/java/org/springframework/docs/integration/mailusage/OrderManager.java new file mode 100644 index 0000000000..b1dab51d79 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/mailusage/OrderManager.java @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.mailusage; + +import org.springframework.docs.integration.mailusagesimple.Order; + +// tag::snippet[] +public interface OrderManager { + + void placeOrder(Order order); +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/Customer.java b/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/Customer.java new file mode 100644 index 0000000000..0d01074c74 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/Customer.java @@ -0,0 +1,32 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.mailusagesimple; + +public class Customer { + + public String getEmailAddress() { + return null; + } + + public String getFirstName() { + return null; + } + + public String getLastName() { + return null; + } +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/MailConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/MailConfiguration.java new file mode 100644 index 0000000000..723340868d --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/MailConfiguration.java @@ -0,0 +1,52 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.mailusagesimple; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.JavaMailSenderImpl; + +@Configuration +public class MailConfiguration { + + // tag::snippet[] + @Bean + JavaMailSender mailSender() { + JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + mailSender.setHost("mail.mycompany.example"); + return mailSender; + } + + @Bean // this is a template message that we can pre-load with default state + SimpleMailMessage templateMessage() { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom("customerservice@mycompany.example"); + message.setSubject("Your order"); + return message; + } + + @Bean + SimpleOrderManager orderManager(JavaMailSender mailSender, SimpleMailMessage templateMessage) { + SimpleOrderManager orderManager = new SimpleOrderManager(); + orderManager.setMailSender(mailSender); + orderManager.setTemplateMessage(templateMessage); + return orderManager; + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/Order.java b/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/Order.java new file mode 100644 index 0000000000..27c53e0d6a --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/Order.java @@ -0,0 +1,28 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.mailusagesimple; + +public class Order { + + public Customer getCustomer() { + return null; + } + + public String getOrderNumber() { + return null; + } +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/SimpleOrderManager.java b/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/SimpleOrderManager.java new file mode 100644 index 0000000000..6835d2f0af --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/mailusagesimple/SimpleOrderManager.java @@ -0,0 +1,64 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.mailusagesimple; + +import org.springframework.docs.integration.mailusage.OrderManager; +import org.springframework.mail.SimpleMailMessage; + +import org.springframework.mail.MailException; +import org.springframework.mail.MailSender; + +// tag::snippet[] +public class SimpleOrderManager implements OrderManager { + + private MailSender mailSender; + private SimpleMailMessage templateMessage; + + public void setMailSender(MailSender mailSender) { + this.mailSender = mailSender; + } + + public void setTemplateMessage(SimpleMailMessage templateMessage) { + this.templateMessage = templateMessage; + } + + @Override + public void placeOrder(Order order) { + + // Do the business calculations... + + // Call the collaborators to persist the order... + + // Create a thread-safe "copy" of the template message and customize it + SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage); + msg.setTo(order.getCustomer().getEmailAddress()); + msg.setText( + "Dear " + order.getCustomer().getFirstName() + + order.getCustomer().getLastName() + + ", thank you for placing order. Your order number is " + + order.getOrderNumber()); + try { + this.mailSender.send(msg); + } + catch (MailException ex) { + // simply log it and go on... + System.err.println(ex.getMessage()); + } + } + +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/schedulingenableannotationsupport/SchedulingConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/schedulingenableannotationsupport/SchedulingConfiguration.java new file mode 100644 index 0000000000..d38b7b4b4c --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/schedulingenableannotationsupport/SchedulingConfiguration.java @@ -0,0 +1,29 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.schedulingenableannotationsupport; + +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; + +// tag::snippet[] +@Configuration +@EnableAsync +@EnableScheduling +public class SchedulingConfiguration { +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorConfiguration.java new file mode 100644 index 0000000000..c3beb0be04 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorConfiguration.java @@ -0,0 +1,41 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.schedulingtaskexecutorusage; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +@Configuration +public class TaskExecutorConfiguration { + + // tag::snippet[] + @Bean + ThreadPoolTaskExecutor taskExecutor() { + ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); + taskExecutor.setCorePoolSize(5); + taskExecutor.setMaxPoolSize(10); + taskExecutor.setQueueCapacity(25); + return taskExecutor; + } + + @Bean + TaskExecutorExample taskExecutorExample(ThreadPoolTaskExecutor taskExecutor) { + return new TaskExecutorExample(taskExecutor); + } + // end::snippet[] +} diff --git a/framework-docs/src/main/java/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorExample.java b/framework-docs/src/main/java/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorExample.java new file mode 100644 index 0000000000..62a156cff1 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorExample.java @@ -0,0 +1,50 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.schedulingtaskexecutorusage; + +import org.springframework.core.task.TaskExecutor; + +// tag::snippet[] +public class TaskExecutorExample { + + private class MessagePrinterTask implements Runnable { + + private String message; + + public MessagePrinterTask(String message) { + this.message = message; + } + + public void run() { + System.out.println(message); + } + } + + private TaskExecutor taskExecutor; + + public TaskExecutorExample(TaskExecutor taskExecutor) { + this.taskExecutor = taskExecutor; + } + + public void printMessages() { + for(int i = 0; i < 25; i++) { + taskExecutor.execute(new MessagePrinterTask("Message" + i)); + } + } +} +// end::snippet[] diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cacheannotationenable/CacheConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cacheannotationenable/CacheConfiguration.kt new file mode 100644 index 0000000000..674b8aad85 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cacheannotationenable/CacheConfiguration.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cacheannotationenable + +import org.springframework.cache.CacheManager +import org.springframework.cache.annotation.EnableCaching +import org.springframework.cache.caffeine.CaffeineCacheManager +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +// tag::snippet[] +@Configuration +@EnableCaching +class CacheConfiguration { + + @Bean + fun cacheManager(): CacheManager { + return CaffeineCacheManager().apply { + setCacheSpecification("...") + } + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CacheConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CacheConfiguration.kt new file mode 100644 index 0000000000..8e85c14bbb --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CacheConfiguration.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cachestoreconfigurationcaffeine + +import org.springframework.cache.CacheManager +import org.springframework.cache.caffeine.CaffeineCacheManager +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + + +@Configuration +class CacheConfiguration { + + // tag::snippet[] + @Bean + fun cacheManager(): CacheManager { + return CaffeineCacheManager() + } + // end::snippet[] +} \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CustomCacheConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CustomCacheConfiguration.kt new file mode 100644 index 0000000000..f91e78a548 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CustomCacheConfiguration.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cachestoreconfigurationcaffeine + +import org.springframework.cache.CacheManager +import org.springframework.cache.caffeine.CaffeineCacheManager +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + + +@Configuration +class CustomCacheConfiguration { + + // tag::snippet[] + @Bean + fun cacheManager(): CacheManager { + return CaffeineCacheManager().apply { + cacheNames = listOf("default", "books") + } + } + // end::snippet[] +} \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationjdk/CacheConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationjdk/CacheConfiguration.kt new file mode 100644 index 0000000000..779b1168eb --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationjdk/CacheConfiguration.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cachestoreconfigurationjdk + +import org.springframework.cache.CacheManager +import org.springframework.cache.concurrent.ConcurrentMapCache +import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean +import org.springframework.cache.support.SimpleCacheManager +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + + +@Configuration +class CacheConfiguration { + + // tag::snippet[] + @Bean + fun defaultCache(): ConcurrentMapCacheFactoryBean { + return ConcurrentMapCacheFactoryBean().apply { + setName("default") + } + } + + @Bean + fun booksCache(): ConcurrentMapCacheFactoryBean { + return ConcurrentMapCacheFactoryBean().apply { + setName("books") + } + } + + @Bean + fun cacheManager(defaultCache: ConcurrentMapCache, booksCache: ConcurrentMapCache): CacheManager { + return SimpleCacheManager().apply { + setCaches(setOf(defaultCache, booksCache)) + } + } + // end::snippet[] +} \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationjsr107/CacheConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationjsr107/CacheConfiguration.kt new file mode 100644 index 0000000000..308f60cd7c --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationjsr107/CacheConfiguration.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cachestoreconfigurationjsr107 + +import org.springframework.cache.jcache.JCacheCacheManager +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import javax.cache.Caching + +@Suppress("UsePropertyAccessSyntax") +@Configuration +class CacheConfiguration { + + // tag::snippet[] + @Bean + fun jCacheManager(): javax.cache.CacheManager { + val cachingProvider = Caching.getCachingProvider() + return cachingProvider.getCacheManager() + } + + @Bean + fun cacheManager(jCacheManager: javax.cache.CacheManager): org.springframework.cache.CacheManager { + return JCacheCacheManager(jCacheManager) + } + // end::snippet[] +} \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationnoop/CacheConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationnoop/CacheConfiguration.kt new file mode 100644 index 0000000000..5fc16ea91e --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/cache/cachestoreconfigurationnoop/CacheConfiguration.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.cache.cachestoreconfigurationnoop + +import org.springframework.cache.CacheManager +import org.springframework.cache.support.CompositeCacheManager +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +class CacheConfiguration { + + private fun jdkCache(): CacheManager? { + return null + } + + private fun gemfireCache(): CacheManager? { + return null + } + + // tag::snippet[] + @Bean + fun cacheManager(jdkCache: CacheManager, gemfireCache: CacheManager): CacheManager { + return CompositeCacheManager().apply { + setCacheManagers(listOf(jdkCache, gemfireCache)) + setFallbackToNoOpCache(true) + } + } + // end::snippet[] +} \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsannotatedsupport/JmsConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsannotatedsupport/JmsConfiguration.kt new file mode 100644 index 0000000000..2972d22898 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsannotatedsupport/JmsConfiguration.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsannotatedsupport + +import jakarta.jms.ConnectionFactory +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.jms.annotation.EnableJms +import org.springframework.jms.config.DefaultJmsListenerContainerFactory +import org.springframework.jms.support.destination.DestinationResolver + +// tag::snippet[] +@Configuration +@EnableJms +class JmsConfiguration { + + @Bean + fun jmsListenerContainerFactory(connectionFactory: ConnectionFactory, destinationResolver: DestinationResolver) = + DefaultJmsListenerContainerFactory().apply { + setConnectionFactory(connectionFactory) + setDestinationResolver(destinationResolver) + setSessionTransacted(true) + setConcurrency("3-10") + } +} +// end::snippet[] diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/AlternativeJmsConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/AlternativeJmsConfiguration.kt new file mode 100644 index 0000000000..9127de9c7a --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/AlternativeJmsConfiguration.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsjcamessageendpointmanager + +import jakarta.jms.MessageListener +import jakarta.resource.spi.ResourceAdapter +import org.apache.activemq.ra.ActiveMQActivationSpec +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.jms.listener.endpoint.JmsMessageEndpointManager + +@Configuration +class AlternativeJmsConfiguration { + + // tag::snippet[] + @Bean + fun jmsMessageEndpointManager( + resourceAdapter: ResourceAdapter, myMessageListener: MessageListener) = JmsMessageEndpointManager().apply { + setResourceAdapter(resourceAdapter) + activationSpec = ActiveMQActivationSpec().apply { + destination = "myQueue" + destinationType = "jakarta.jms.Queue" + } + messageListener = myMessageListener + } + // end::snippet[] +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/JmsConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/JmsConfiguration.kt new file mode 100644 index 0000000000..d7fecece98 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/JmsConfiguration.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsjcamessageendpointmanager + +import jakarta.jms.MessageListener +import jakarta.resource.spi.ResourceAdapter +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.jms.listener.endpoint.JmsActivationSpecConfig +import org.springframework.jms.listener.endpoint.JmsMessageEndpointManager + +@Configuration +class JmsConfiguration { + + // tag::snippet[] + @Bean + fun jmsMessageEndpointManager( + resourceAdapter: ResourceAdapter, myMessageListener: MessageListener) = JmsMessageEndpointManager().apply { + setResourceAdapter(resourceAdapter) + activationSpecConfig = JmsActivationSpecConfig().apply { + destinationName = "myQueue" + } + messageListener = myMessageListener + } + // end::snippet[] +} \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasync/ExampleListener.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasync/ExampleListener.kt new file mode 100644 index 0000000000..002813ae7b --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasync/ExampleListener.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasync + +import jakarta.jms.JMSException +import jakarta.jms.Message +import jakarta.jms.MessageListener +import jakarta.jms.TextMessage + +// tag::snippet[] +class ExampleListener : MessageListener { + + override fun onMessage(message: Message) { + if (message is TextMessage) { + try { + println(message.text) + } catch (ex: JMSException) { + throw RuntimeException(ex) + } + } else { + throw IllegalArgumentException("Message must be of type TextMessage") + } + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasync/JmsConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasync/JmsConfiguration.kt new file mode 100644 index 0000000000..2b0975e277 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasync/JmsConfiguration.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasync + +import jakarta.jms.ConnectionFactory +import jakarta.jms.Destination +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.jms.listener.DefaultMessageListenerContainer + +@Configuration +class JmsConfiguration { + + // tag::snippet[] + @Bean + fun messageListener() = ExampleListener() + + @Bean + fun jmsContainer(connectionFactory: ConnectionFactory, destination: Destination, messageListener: ExampleListener) = + DefaultMessageListenerContainer().apply { + setConnectionFactory(connectionFactory) + setDestination(destination) + setMessageListener(messageListener) + } + // end::snippet[] +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultMessageDelegate.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultMessageDelegate.kt new file mode 100644 index 0000000000..c28c9a1ec6 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultMessageDelegate.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter + +import java.io.Serializable + +// tag::snippet[] +class DefaultMessageDelegate : MessageDelegate { + + override fun handleMessage(message: String) { + // ... + } + + override fun handleMessage(message: Map<*, *>) { + // ... + } + + override fun handleMessage(message: ByteArray) { + // ... + } + + override fun handleMessage(message: Serializable) { + // ... + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultResponsiveTextMessageDelegate.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultResponsiveTextMessageDelegate.kt new file mode 100644 index 0000000000..fd4c58b6da --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultResponsiveTextMessageDelegate.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter + +import jakarta.jms.TextMessage + +// tag::snippet[] +class DefaultResponsiveTextMessageDelegate : ResponsiveTextMessageDelegate { + + override fun receive(message: TextMessage): String { + return "message" + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultTextMessageDelegate.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultTextMessageDelegate.kt new file mode 100644 index 0000000000..3a4ab53c5e --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/DefaultTextMessageDelegate.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter + +import org.springframework.web.socket.TextMessage + +// tag::snippet[] +class DefaultTextMessageDelegate : TextMessageDelegate { + + override fun receive(message: TextMessage) { + // ... + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/JmsConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/JmsConfiguration.kt new file mode 100644 index 0000000000..14ff4c8c51 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/JmsConfiguration.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter + +import jakarta.jms.ConnectionFactory +import jakarta.jms.Destination +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.docs.integration.jms.jmsreceivingasync.ExampleListener +import org.springframework.jms.listener.DefaultMessageListenerContainer +import org.springframework.jms.listener.adapter.MessageListenerAdapter + +@Configuration +class JmsConfiguration { + + // tag::snippet[] + @Bean + fun messageListener(messageDelegate: DefaultMessageDelegate): MessageListenerAdapter { + return MessageListenerAdapter(messageDelegate) + } + + @Bean + fun jmsContainer(connectionFactory: ConnectionFactory, destination: Destination, messageListener: ExampleListener) = + DefaultMessageListenerContainer().apply { + setConnectionFactory(connectionFactory) + setDestination(destination) + setMessageListener(messageListener) + } + // end::snippet[] +} \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageDelegate.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageDelegate.kt new file mode 100644 index 0000000000..fe55a8e740 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageDelegate.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter + +import java.io.Serializable + +// tag::snippet[] +interface MessageDelegate { + fun handleMessage(message: String) + fun handleMessage(message: Map<*, *>) + fun handleMessage(message: ByteArray) + fun handleMessage(message: Serializable) +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageListenerConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageListenerConfiguration.kt new file mode 100644 index 0000000000..b77d0dc856 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageListenerConfiguration.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.jms.listener.adapter.MessageListenerAdapter + +@Configuration +class MessageListenerConfiguration { + + // tag::snippet[] + @Bean + fun messageListener(messageDelegate: DefaultTextMessageDelegate) = MessageListenerAdapter(messageDelegate).apply { + setDefaultListenerMethod("receive") + // We don't want automatic message context extraction + setMessageConverter(null) + } + // end::snippet[] +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/ResponsiveTextMessageDelegate.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/ResponsiveTextMessageDelegate.kt new file mode 100644 index 0000000000..7f6ba5eacc --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/ResponsiveTextMessageDelegate.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter + +import jakarta.jms.TextMessage + +// tag::snippet[] +interface ResponsiveTextMessageDelegate { + + // Notice the return type... + fun receive(message: TextMessage): String +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/TextMessageDelegate.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/TextMessageDelegate.kt new file mode 100644 index 0000000000..d90a04b9b3 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/TextMessageDelegate.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmsreceivingasyncmessagelisteneradapter + +import org.springframework.web.socket.TextMessage + +// tag::snippet[] +interface TextMessageDelegate { + fun receive(message: TextMessage) +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmstxparticipation/ExternalTxJmsConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmstxparticipation/ExternalTxJmsConfiguration.kt new file mode 100644 index 0000000000..1805e2c373 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmstxparticipation/ExternalTxJmsConfiguration.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmstxparticipation + +import jakarta.jms.ConnectionFactory +import jakarta.jms.Destination +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.docs.integration.jms.jmsreceivingasync.ExampleListener +import org.springframework.jms.listener.DefaultMessageListenerContainer +import org.springframework.transaction.jta.JtaTransactionManager + +@Configuration +class ExternalTxJmsConfiguration { + + // tag::transactionManagerSnippet[] + @Bean + fun transactionManager() = JtaTransactionManager() + // end::transactionManagerSnippet[] + + // tag::jmsContainerSnippet[] + @Bean + fun jmsContainer(connectionFactory: ConnectionFactory, destination: Destination, messageListener: ExampleListener, + transactionManager: JtaTransactionManager) = + DefaultMessageListenerContainer().apply { + setConnectionFactory(connectionFactory) + setDestination(destination) + setMessageListener(messageListener) + setTransactionManager(transactionManager) + } + // end::jmsContainerSnippet[] +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmstxparticipation/JmsConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmstxparticipation/JmsConfiguration.kt new file mode 100644 index 0000000000..ce0a5b085c --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jms/jmstxparticipation/JmsConfiguration.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jms.jmstxparticipation + +import jakarta.jms.ConnectionFactory +import jakarta.jms.Destination +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.docs.integration.jms.jmsreceivingasync.ExampleListener +import org.springframework.jms.listener.DefaultMessageListenerContainer + +@Configuration +class JmsConfiguration { + + // tag::snippet[] + @Bean + fun jmsContainer(connectionFactory: ConnectionFactory, destination: Destination, messageListener: ExampleListener) = + DefaultMessageListenerContainer().apply { + setConnectionFactory(connectionFactory) + setDestination(destination) + setMessageListener(messageListener) + isSessionTransacted = true + } + // end::snippet[] +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/CustomJmxConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/CustomJmxConfiguration.kt new file mode 100644 index 0000000000..98d9e5f045 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/CustomJmxConfiguration.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jmx.jmxcontextmbeanexport + +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.EnableMBeanExport + +// tag::snippet[] +@Configuration +@EnableMBeanExport(server="myMBeanServer", defaultDomain="myDomain") +class CustomJmxConfiguration +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/JmxConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/JmxConfiguration.kt new file mode 100644 index 0000000000..9f88e70ced --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/JmxConfiguration.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jmx.jmxcontextmbeanexport + +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.EnableMBeanExport + +// tag::snippet[] +@Configuration +@EnableMBeanExport +class JmxConfiguration +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxexporting/JmxConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxexporting/JmxConfiguration.kt new file mode 100644 index 0000000000..1f3210110e --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxexporting/JmxConfiguration.kt @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jmx.jmxexporting + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.jmx.export.MBeanExporter + +// tag::snippet[] +@Configuration +class JmxConfiguration { + + @Bean + fun exporter(testBean: JmxTestBean) = MBeanExporter().apply { + setBeans(mapOf("bean:name=testBean1" to testBean)) + } + + @Bean + fun testBean() = JmxTestBean().apply { + name = "TEST" + age = 100 + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxexporting/JmxTestBean.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxexporting/JmxTestBean.kt new file mode 100644 index 0000000000..a4936ed4d9 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/jmx/jmxexporting/JmxTestBean.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.jmx.jmxexporting + +// tag::snippet[] +class JmxTestBean : IJmxTestBean { + + private lateinit var name: String + private var age = 0 + + override fun getAge(): Int { + return age + } + + override fun setAge(age: Int) { + this.age = age + } + + override fun setName(name: String) { + this.name = name + } + + override fun getName(): String { + return name + } + + override fun add(x: Int, y: Int): Int { + return x + y + } + + override fun dontExposeMe() { + throw RuntimeException() + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/mailusage/OrderManager.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/mailusage/OrderManager.kt new file mode 100644 index 0000000000..08f7b80d12 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/mailusage/OrderManager.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.mailusage + +import org.springframework.docs.integration.mailusagesimple.Order + +// tag::snippet[] +interface OrderManager { + + fun placeOrder(order: Order) +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/mailusagesimple/MailConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/mailusagesimple/MailConfiguration.kt new file mode 100644 index 0000000000..a4f0378da9 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/mailusagesimple/MailConfiguration.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.mailusagesimple + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.mail.SimpleMailMessage +import org.springframework.mail.javamail.JavaMailSender +import org.springframework.mail.javamail.JavaMailSenderImpl + +@Configuration +class MailConfiguration { + + // tag::snippet[] + @Bean + fun mailSender(): JavaMailSender { + return JavaMailSenderImpl().apply { + host = "mail.mycompany.example" + } + } + + @Bean // this is a template message that we can pre-load with default state + fun templateMessage() = SimpleMailMessage().apply { + from = "customerservice@mycompany.example" + subject = "Your order" + } + + + @Bean + fun orderManager(javaMailSender: JavaMailSender, simpleTemplateMessage: SimpleMailMessage) = SimpleOrderManager().apply { + mailSender = javaMailSender + templateMessage = simpleTemplateMessage + } + // end::snippet[] +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/mailusagesimple/SimpleOrderManager.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/mailusagesimple/SimpleOrderManager.kt new file mode 100644 index 0000000000..7be7aa437d --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/mailusagesimple/SimpleOrderManager.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.mailusagesimple + +import org.springframework.docs.integration.mailusage.OrderManager +import org.springframework.mail.MailException +import org.springframework.mail.MailSender +import org.springframework.mail.SimpleMailMessage + +// tag::snippet[] +class SimpleOrderManager : OrderManager { + + lateinit var mailSender: MailSender + lateinit var templateMessage: SimpleMailMessage + + override fun placeOrder(order: Order) { + // Do the business calculations... + + // Call the collaborators to persist the order... + + // Create a thread-safe "copy" of the template message and customize it + + val msg = SimpleMailMessage(this.templateMessage) + msg.setTo(order.customer.emailAddress) + msg.text = ("Dear " + order.customer.firstName + + order.customer.lastName + + ", thank you for placing order. Your order number is " + + order.orderNumber) + try { + mailSender.send(msg) + } catch (ex: MailException) { + // simply log it and go on... + System.err.println(ex.message) + } + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/schedulingenableannotationsupport/SchedulingConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/schedulingenableannotationsupport/SchedulingConfiguration.kt new file mode 100644 index 0000000000..4faed328fb --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/schedulingenableannotationsupport/SchedulingConfiguration.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.schedulingenableannotationsupport + +import org.springframework.context.annotation.Configuration +import org.springframework.scheduling.annotation.EnableAsync +import org.springframework.scheduling.annotation.EnableScheduling + +// tag::snippet[] +@Configuration +@EnableAsync +@EnableScheduling +class SchedulingConfiguration +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorConfiguration.kt new file mode 100644 index 0000000000..1a2dd1a886 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorConfiguration.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.schedulingtaskexecutorusage + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor + +@Configuration +class TaskExecutorConfiguration { + + // tag::snippet[] + @Bean + fun taskExecutor() = ThreadPoolTaskExecutor().apply { + corePoolSize = 5 + maxPoolSize = 10 + queueCapacity = 25 + } + + @Bean + fun taskExecutorExample(taskExecutor: ThreadPoolTaskExecutor) = TaskExecutorExample(taskExecutor) + // end::snippet[] +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorExample.kt b/framework-docs/src/main/kotlin/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorExample.kt new file mode 100644 index 0000000000..4ec8a261dd --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorExample.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.docs.integration.schedulingtaskexecutorusage + +import org.springframework.core.task.TaskExecutor + +// tag::snippet[] +class TaskExecutorExample(private val taskExecutor: TaskExecutor) { + + private inner class MessagePrinterTask(private val message: String) : Runnable { + override fun run() { + println(message) + } + } + + fun printMessages() { + for (i in 0..24) { + taskExecutor.execute( + MessagePrinterTask( + "Message$i" + ) + ) + } + } +} +// end::snippet[] \ No newline at end of file diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cacheannotationenable/CacheConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cacheannotationenable/CacheConfiguration.xml new file mode 100644 index 0000000000..af465a230c --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cacheannotationenable/CacheConfiguration.xml @@ -0,0 +1,15 @@ + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CacheConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CacheConfiguration.xml new file mode 100644 index 0000000000..47d8906148 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CacheConfiguration.xml @@ -0,0 +1,10 @@ + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CustomCacheConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CustomCacheConfiguration.xml new file mode 100644 index 0000000000..9ce1f5af47 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationcaffeine/CustomCacheConfiguration.xml @@ -0,0 +1,17 @@ + + + + + + + default + books + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationjdk/CacheConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationjdk/CacheConfiguration.xml new file mode 100644 index 0000000000..3aadd751dc --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationjdk/CacheConfiguration.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationjsr107/CacheConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationjsr107/CacheConfiguration.xml new file mode 100644 index 0000000000..6231658aa2 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationjsr107/CacheConfiguration.xml @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationnoop/CacheConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationnoop/CacheConfiguration.xml new file mode 100644 index 0000000000..a87f3da9f7 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/cache/cachestoreconfigurationnoop/CacheConfiguration.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsannotatedsupport/JmsConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsannotatedsupport/JmsConfiguration.xml new file mode 100644 index 0000000000..42d597832c --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsannotatedsupport/JmsConfiguration.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/AlternativeJmsConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/AlternativeJmsConfiguration.xml new file mode 100644 index 0000000000..2cb1532c67 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/AlternativeJmsConfiguration.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/JmsConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/JmsConfiguration.xml new file mode 100644 index 0000000000..a14ad0bff8 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsjcamessageendpointmanager/JmsConfiguration.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsreceivingasync/JmsConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsreceivingasync/JmsConfiguration.xml new file mode 100644 index 0000000000..c7690e4347 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsreceivingasync/JmsConfiguration.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/JmsConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/JmsConfiguration.xml new file mode 100644 index 0000000000..6471dad0f1 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/JmsConfiguration.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageListenerConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageListenerConfiguration.xml new file mode 100644 index 0000000000..e2fab9723c --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmsreceivingasyncmessagelisteneradapter/MessageListenerConfiguration.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmstxparticipation/ExternalTxJmsConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmstxparticipation/ExternalTxJmsConfiguration.xml new file mode 100644 index 0000000000..07668236f9 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmstxparticipation/ExternalTxJmsConfiguration.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmstxparticipation/JmsConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmstxparticipation/JmsConfiguration.xml new file mode 100644 index 0000000000..e0c89287a6 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jms/jmstxparticipation/JmsConfiguration.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/CustomJmxConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/CustomJmxConfiguration.xml new file mode 100644 index 0000000000..5a3017cfc8 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/CustomJmxConfiguration.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/JmxConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/JmxConfiguration.xml new file mode 100644 index 0000000000..c1b7fb50d3 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jmx/jmxcontextmbeanexport/JmxConfiguration.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/jmx/jmxexporting/JmxConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/jmx/jmxexporting/JmxConfiguration.xml new file mode 100644 index 0000000000..692265fa60 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/jmx/jmxexporting/JmxConfiguration.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/mailusagesimple/MailConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/mailusagesimple/MailConfiguration.xml new file mode 100644 index 0000000000..a2259b165a --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/mailusagesimple/MailConfiguration.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/schedulingenableannotationsupport/SchedulingConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/schedulingenableannotationsupport/SchedulingConfiguration.xml new file mode 100644 index 0000000000..aeb0b970a6 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/schedulingenableannotationsupport/SchedulingConfiguration.xml @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/framework-docs/src/main/resources/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorConfiguration.xml new file mode 100644 index 0000000000..a0331c3089 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/integration/schedulingtaskexecutorusage/TaskExecutorConfiguration.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + +