spring JMS理论、主题模式和队列模式

本文介绍如何使用Spring JMS实现消息队列。主要介绍了Spring提供的ConnectionFactory接口及其实现类SingleConnectionFactory和CachingConnectionFactory,JmsTemplate的使用方法,以及如何配置消息监听器。并提供了基于队列模式的生产者和消费者的Maven项目搭建步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring提供的接口

  • ConnectionFactory 用于管理连接的连接工厂
    • spring提供的连接池
    • JMSTemplate 每次都会重新创建连接,会话和productor
    • spring中提供了SingleConnectionFactory和CachingConnectionFactory
      • SingleConnectionFactory 对于建立JMS服务器连接的请求只会返回一个同一个Connection,也就是说在整个应用中只会使用一个连接进行操作
      • CachingConnectionFactory 继承了SingleConnectionFactory所以它拥有了SingleConnectionFactory它所有的功能,同时还新增了缓存功能,缓存producer和consumer
  • JmsTemplate用户发送和接受消息的模板类
    • 是spring提供的,只需要向spring容器内注册这个类就可以使用jmsTemplate方便操作jms
    • JmsTemplate 是线程安全的,可以在整个应用范围使用,但是并不代表整个应用中只能使用一个,我们可以创建多个
  • MessageListerner 消息监听器
    • 实现一个onMessage方法,该方法只接受一个message参数
      ### 队列模式
  • 创建maven项目
  • 导入依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.winter.jms</groupId>
    <artifactId>spring-jms</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>4.2.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-core</artifactId>
                <version>5.7.0</version>
            <exclusions>
                <exclusion><!-- 因为我们自己已经引用了这个包,并且这个包中也附带了这个包,所以需要排除掉 -->
                    <artifactId>spring-context</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <!-- jdb编译版本 -->
    <build>

        <plugins>
            <!-- compiler插件, 设定JDK版本 -->
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!-- 以上是jdk编译版本 -->
</project>
  • 创建common.xml
<?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: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.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
       <!-- 开启注解支持,也就是支持auwired  resource等注解,但是spring扫描组件内同样可以做到,并且还可以加载其他的注解 -->
       <context:annotation-config/>
        <!-- activemq 提供的连接工厂 -->
       <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="tcp://192.168.79.134:61616"></property>
       </bean>
        <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
        </bean>
       <!-- 一个队列 的 目的地 -->
        <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
            <!--  通过构造函数注入 -->
            <constructor-arg value="queue"/>
        </bean>    
 </beans>
  • 创建productor.xml
<?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: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.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
       <!-- 导入公共配置 -->
       <import resource="common.xml"/>
         <!-- 配置JMSTemplate,用于发送消息 -->
        <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
            <property name="connectionFactory" ref="connectionFactory"/>
        </bean>
        <!-- 注册生产者 -->
        <bean id="productor" class="com.td.productor.ProductorImp"></bean>
 </beans>
  • 创建consumer.xml
<?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: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.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 导入公共配置 -->
    <import resource="common.xml" />
    <!-- 配置消息监听器 -->
    <bean id="consumerMessageListener" class="com.td.consumer.ConsumerMessageListener" />
    <!-- 配置消息监听器容器 -->
    <bean id="jmsContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <!-- 消息监听地址 -->
        <property name="destination" ref="queueDestination" />
        <!-- 消息监听器 -->
        <property name="messageListener" ref="consumerMessageListener" />
    </bean>
</beans>
  • 创建生产者服务这里写图片描述
  • 实现类这里写图片描述
  • 服务监听器这里写图片描述
  • 生产者启动类:这里写图片描述
  • 消费者启动类这里写图片描述
  • 测试:
    • 运行生产者启动类:这里写图片描述
    • 启动消费者启动类:这里写图片描述

主题模式

  • 只需要将common.xml 文件中的目标类改为Topic类型的这里写图片描述
  • 测试:
    • 启动两个消费者: 这里写图片描述
    • 启动生产者:这里写图片描述
    • 查看服务器:这里写图片描述
内容概要:本文针对火电厂参与直购交易挤占风电上网空间的问题,提出了一种风火打捆参与大用户直购交易的新模式。通过分析可再生能源配额机制下的双边博弈关系,建立了基于动态非合作博弈理论的博弈模型,以直购电价直购电量为决策变量,实现双方收益均衡最大化。论文论证了纳什均衡的存在性,并提出了基于纳什谈判法的风-火利益分配方法。算例结果表明,该模式能够增加各方收益、促进风电消纳并提高电网灵活性。文中详细介绍了模型构建、成本计算博弈均衡的实现过程,并通过Python代码复现了模型,包括参数定义、收益函数、纳什均衡求解、利益分配及可视化分析等功能。 适合人群:电力系统研究人员、能源政策制定者、从事电力市场交易的工程师分析师。 使用场景及目标:①帮助理解风火打捆参与大用户直购交易的博弈机制;②为电力市场设计提供理论依据技术支持;③评估不同政策(如可再生能源配额)对电力市场的影响;④通过代码实现可视化工具辅助教学研究。 其他说明:该研究不仅提供了理论分析,还通过详细的代码实现算例验证了模型的有效性,为实际应用提供了参考。此外,论文还探讨了不同场景下的敏感性分析,如证书价格、风电比例等对市场结果的影响,进一步丰富了研究内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值