【ActiveMQ】ActiveMQ与Spring实现发邮件

2024-08-26 22:32

本文主要是介绍【ActiveMQ】ActiveMQ与Spring实现发邮件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

消费者如何知道生产者的动静?当生产者发送了消息到ActiveMQ中的时候,消费者是如何轻松的获得该消息的。

这是由JMS提供的三种消息监听器实现的。我们先来了解基础概念:


1、MessageListener


这是JMS的一个接口,定义了一个用于处理接受到消息的onMessage方法,该接口仅仅一个这样的方法,

也仅仅有Message一个参数。他就是为了接受消息的。请见以下代码:

package com.consume;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;public class ConsumerMessageListener implements MessageListener{public void onMessage(Message message) {TextMessage textMessage=(TextMessage) message;System.out.println("哈哈哈哈哈");try {System.out.println("消费者:"+textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}}

2、SessionAwareMessageListener


这个是由spring为我们提供的,他不是JMS的MessageListener,这个接口仅仅有一个方法,那就是onMessage

方法,有2个参数,一个参数用来接受当前的消息message,另一个发送消息的sesson对象。其中session对象就是

用来告诉消息的提供者发送回复消息的。

请看这个接口的定义:



请见以下代码:

@Component
public class ConsumerSessionAwareMessageListener implements SessionAwareMessageListener<Message> {private static final Log log = LogFactory.getLog(ConsumerSessionAwareMessageListener.class);@Autowiredprivate JmsTemplate activeMqJmsTemplate;@Autowiredprivate Destination sessionAwareQueue;@Autowiredprivate MailBiz bailBiz;public synchronized void onMessage(Message message, Session session) {try {ActiveMQTextMessage msg = (ActiveMQTextMessage) message;final String ms = msg.getText();log.info("==>receive message:" + ms);MailParam mailParam = JSONObject.parseObject(ms, MailParam.class);// 转换成相应的对象if (mailParam == null) {return;}try {bailBiz.mailSend(mailParam);} catch (Exception e) {// 发送异常,重新放回队列
//				activeMqJmsTemplate.send(sessionAwareQueue, new MessageCreator() {
//					public Message createMessage(Session session) throws JMSException {
//						return session.createTextMessage(ms);
//					}
//				});log.error("==>MailException:", e);}} catch (Exception e) {log.error("==>", e);}}
}


Spring和ActiveMQ结合,首先需要在配置文件中配置,也就是当配置文件一加载,开始实例化监听器,然后就开始监听生产者的消息了。所以需要配置<bean>.


3、MessageListenerAdapter

    由以上可知,这个接口实现了上面的2个接口,所以除了有上面的方法外,还有很多其他的方法。


 邮件发送的例子:

我们用邮件发送的例子来介绍一下生产者和消费者的行为。

首先生产者定义发送邮件的内容,和发送给谁,然后消费者运用接听来时时刻刻监听是否发送的人是自己,假如是自己,则受到消息。


首先我们来建立生产者工程


1、新建maven工程,引用spring和ActiveMQ的jar包


<dependencies><dependency><groupId>antlr</groupId><artifactId>antlr</artifactId></dependency><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId></dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><classifier>jdk15</classifier><scope>compile</scope></dependency><dependency><groupId>ognl</groupId><artifactId>ognl</artifactId></dependency><dependency><groupId>oro</groupId><artifactId>oro</artifactId></dependency><dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId></dependency><dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId></dependency><dependency><groupId>commons-digester</groupId><artifactId>commons-digester</artifactId></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></dependency><dependency><groupId>commons-validator</groupId><artifactId>commons-validator</artifactId></dependency><dependency><groupId>dom4j</groupId><artifactId>dom4j</artifactId></dependency><dependency><groupId>net.sf.ezmorph</groupId><artifactId>ezmorph</artifactId></dependency><dependency><groupId>javassist</groupId><artifactId>javassist</artifactId></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><!-- Common Dependency End --><!-- Spring Dependency Begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-oxm</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-pool</artifactId></dependency></dependencies>

       2、新建配置文件,分别是spring的配置文件和mq的配置文件


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"default-autowire="byName" default-lazy-init="false"><!-- 采用注释的方式配置bean --><context:annotation-config /><!-- 配置要扫描的包 --><context:component-scan base-package="mq.demo" /><!-- 读入配置属性文件 --><context:property-placeholder location="classpath:mq.properties" /><!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 --><aop:aspectj-autoproxy proxy-target-class="true" />	<import resource="spring-mq.xml" />
</beans>

         mq的配置文件:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"default-autowire="byName" default-lazy-init="false"><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><!-- ActiveMQ服务地址 --><property name="brokerURL" value="${mq.brokerURL}" /><property name="userName" value="${mq.userName}"></property><property name="password" value="${mq.password}"></property> </bean><!-- ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。要依赖于 activemq-pool包--><bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"><property name="connectionFactory" ref="targetConnectionFactory" /><property name="maxConnections" value="${mq.pool.maxConnections}" /></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="pooledConnectionFactory" /></bean><!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><!-- 队列模板 --><bean id="activeMqJmsTemplate" class="org.springframework.jms.core.JmsTemplate">  <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  <property name="connectionFactory" ref="connectionFactory"/>  <property name="defaultDestinationName" value="${queueName}"></property></bean> </beans>


          mq.properties文件的内容:


## MQ
mq.brokerURL=tcp\://192.168.120.153\:61616
mq.userName=lifen
mq.password=lifen.123
mq.pool.maxConnections=10
#queueName
queueName=fen.email.demo


3、由于是发送邮件,新建一个邮件的封装实体类,类名为MailParam


public class MailParam {/** 发件人 **/private String from;/** 收件人 **/private String to;/** 主题 **/private String subject;/** 邮件内容 **/private String content;public MailParam() {}public MailParam(String to, String subject, String content) {this.to = to;this.subject = subject;this.content = content;}public String getFrom() {return from;}public void setFrom(String from) {this.from = from;}public String getTo() {return to;}public void setTo(String to) {this.to = to;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}
}


       4、 接下来就是MQ的生产者代码:


@Service("mqProducer")
public class MQProducer {@Autowiredprivate JmsTemplate activeMqJmsTemplate;/*** 发送消息.* @param mail */public void sendMessage(final MailParam mail) {activeMqJmsTemplate.send(new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(JSONObject.toJSONString(mail));}});}}

5、 新建测试类,我们来测试一下:


public class MQProducerTest {private static final Log log = LogFactory.getLog(MQProducerTest.class);public static void main(String[] args) {try {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");context.start();MQProducer mqProducer = (MQProducer) context.getBean("mqProducer");// 邮件发送MailParam mail = new MailParam();mail.setTo("lf0908lf@163.com");mail.setSubject("ActiveMQ测试");mail.setContent("通过ActiveMQ异步发送邮件!一定会成功的");mqProducer.sendMessage(mail);context.stop();} catch (Exception e) {log.error("==>MQ context start error:", e);System.exit(0);} finally {log.info("===>System.exit");System.exit(0);}}
}



我们在ActiveMQ的管理页面,就可以看到一个名为fen.email.demo的队列了。


         接下来我们建立消费者的工程,

1、同样引用jar包,和生产者的jar包一样。

2、接着就是配置文件

spring-context的配置文件:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"default-autowire="byName" default-lazy-init="false"><!-- 采用注释的方式配置bean --><context:annotation-config /><!-- 配置要扫描的包 --><context:component-scan base-package="mq.demo" /><!-- 读入配置属性文件 --><context:property-placeholder location="classpath:mq.properties,classpath:mail.properties" /><!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 --><aop:aspectj-autoproxy proxy-target-class="true" />	<import resource="spring-mq.xml" /><import resource="spring-mail.xml" /></beans>

spring-mail的配置文件:


<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"><!-- Spring提供的发送电子邮件的高级抽象类 --><bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="host" value="${mail.host}" /><property name="username" value="${mail.username}" /><property name="password" value="${mail.password}" /><property name="defaultEncoding" value="UTF-8"></property><property name="javaMailProperties"><props><prop key="mail.smtp.auth">${mail.smtp.auth}</prop><prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop></props></property></bean><bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"><property name="from"><value>${mail.default.from}</value></property></bean><!-- 配置线程池 --><bean id="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"><!-- 线程池维护线程的最少数量 --><property name="corePoolSize" value="5" /><!-- 线程池维护线程所允许的空闲时间 --><property name="keepAliveSeconds" value="30000" /><!-- 线程池维护线程的最大数量 --><property name="maxPoolSize" value="50" /><!-- 线程池所使用的缓冲队列 --><property name="queueCapacity" value="100" /></bean></beans>

spring-mq的配置文件:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"default-autowire="byName" default-lazy-init="false"><!--default-lazy-init spring 在启动的时候,会默认加载整个对象实例图,从初始化ACTION配置,到service再到dao配置,乃至到数据库的连接。事务等等,当设置为false的时候,会大大减少初始化的时间,减少spring的启动速度。在开发阶段,可以设置为false但是到了生产环境中,设置为true--><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><!-- ActiveMQ服务地址 --><property name="brokerURL" value="${mq.brokerURL}" /><property name="userName" value="${mq.userName}"></property><property name="password" value="${mq.password}"></property> </bean><!-- ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。要依赖于 activemq-pool包--><bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"><property name="connectionFactory" ref="targetConnectionFactory" /><property name="maxConnections" value="${mq.pool.maxConnections}" /></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="pooledConnectionFactory" /></bean><!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><!-- 队列模板 --><bean id="activeMqJmsTemplate" class="org.springframework.jms.core.JmsTemplate">  <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  <property name="connectionFactory" ref="connectionFactory"/>  <property name="defaultDestinationName" value="${queueName}"></property></bean> <!--这个是sessionAwareQueue目的地 --><bean id="sessionAwareQueue" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg><value>${queueName}</value></constructor-arg></bean><!-- 可以获取session的MessageListener --><bean id="consumerSessionAwareMessageListener" class="mq.demo.mqtest.listener.ConsumerSessionAwareMessageListener"></bean><bean id="sessionAwareListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="sessionAwareQueue" /><property name="messageListener" ref="consumerSessionAwareMessageListener" /></bean></beans>

下面看一下文本文件:


mail.properties


#SMTP服务配置
#mail.host=smtp.qq.com
mail.host=smtp.163.com
mail.username=&&&&&   #邮箱的账号
#mail.port=25
mail.password=&&&&&  #邮箱号的密码
mail.smtp.auth=true
mail.smtp.timeout=30000
mail.default.from=&&&&  #邮箱的账号

mq.properties

## MQ
mq.brokerURL=tcp\://192.168.120.153\:61616
mq.userName=lifen
mq.password=lifen.123
mq.pool.maxConnections=10
#queueName
queueName=fen.email.demo



3、同时也有一个封装邮件参数的实体,和生产者一样,不再叙述。

4、队列监听器,时时刻刻监听者生产端:


@Component
public class ConsumerSessionAwareMessageListener implements SessionAwareMessageListener<Message> {private static final Log log = LogFactory.getLog(ConsumerSessionAwareMessageListener.class);@Autowiredprivate JmsTemplate activeMqJmsTemplate;@Autowiredprivate Destination sessionAwareQueue;@Autowiredprivate MailBiz bailBiz;public synchronized void onMessage(Message message, Session session) {try {ActiveMQTextMessage msg = (ActiveMQTextMessage) message;final String ms = msg.getText();log.info("==>receive message:" + ms);MailParam mailParam = JSONObject.parseObject(ms, MailParam.class);// 转换成相应的对象if (mailParam == null) {return;}try {bailBiz.mailSend(mailParam);} catch (Exception e) {// 发送异常,重新放回队列
//				activeMqJmsTemplate.send(sessionAwareQueue, new MessageCreator() {
//					public Message createMessage(Session session) throws JMSException {
//						return session.createTextMessage(ms);
//					}
//				});log.error("==>MailException:", e);}} catch (Exception e) {log.error("==>", e);}}
}


5、邮件的业务逻辑类:


@Component("mailBiz")
public class MailBiz {@Autowiredprivate JavaMailSender mailSender;// spring配置中定义@Autowiredprivate SimpleMailMessage simpleMailMessage;// spring配置中定义@Autowiredprivate ThreadPoolTaskExecutor threadPool;/*** 发送模板邮件* * @param mailParamTemp需要设置四个参数*            templateName,toMail,subject,mapModel* @throws Exception* */public void mailSend(final MailParam mailParam) {threadPool.execute(new Runnable() {public void run() {try {simpleMailMessage.setFrom(simpleMailMessage.getFrom()); // 发送人,从配置文件中取得simpleMailMessage.setTo(mailParam.getTo()); // 接收人simpleMailMessage.setSubject(mailParam.getSubject());simpleMailMessage.setText(mailParam.getContent());mailSender.send(simpleMailMessage);} catch (MailException e) {throw e;}}});}
}


6、我们测试一下消费者,由于是加载配置文件的时候自动启动监听机制,开始监听,所以我们仅仅需要加载配置文件即可:


public class MQConsumerTest {private static final Log log = LogFactory.getLog(MQConsumer.class);public static void main(String[] args) {try {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");context.start();} catch (Exception e) {log.error("==>MQ context start error:", e);System.exit(0);}}
}


如图:

                          

提醒:

在发送邮件的时候,需要开启以下的服务,要不然会报认证失败的错误,同时也要保证上面的用户名和密码都是正确的。

                 


                ActiveMQ的例子还是比较简单的。

这篇关于【ActiveMQ】ActiveMQ与Spring实现发邮件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1109860

相关文章

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码