吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 706|回复: 2
收起左侧

[已解决] Servlet中使用ActiveMQ报错javax.jms.JMSException: Cannot send, channel has alr...

[复制链接]
majunwang 发表于 2023-4-26 13:25
本帖最后由 majunwang 于 2023-4-27 10:04 编辑

[Java] 纯文本查看 复制代码
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.*;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

@WebServlet(name = "SendOrderServlet", value = "/SendOrderServlet")
public class SendOrderServlet extends HttpServlet {
    private Connection connection;
    private Session session;
    private MessageProducer messageProducer;
    private void setup() throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://10.100.164.50:61616");
        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("order-queue");
        messageProducer = session.createProducer(queue);
        connection.start();
    }
    private void sendMessage(String message) throws JMSException {
        TextMessage textMessage = session.createTextMessage(message);
        messageProducer.send(textMessage);
    }
    private void cleanup() throws JMSException {
        if (messageProducer != null) {
            messageProducer.close();
        }
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
    @Override
    public void init() throws ServletException {
        super.init();
        try {
            setup();
        } catch (JMSException e) {
            throw new ServletException("初始化producer失败!", e);
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String orderId = request.getParameter("order_id");
        String productName = request.getParameter("product_name");
        int quantity = Integer.parseInt(request.getParameter("quantity"));
        float price = Float.parseFloat(request.getParameter("price"));
        String address = request.getParameter("address");

        Order order = new Order(orderId, productName, quantity, price, address);
        String orderJSON = JSONObject.toJSONString(order);

        String result = "发送成功!";
        try {
            sendMessage(orderJSON);
        } catch (Exception e) {
            result = "发送失败!原因:" + e;
        }

        request.setAttribute("result", result);
        request.getRequestDispatcher("/send_orders.jsp").forward(request, response);
    }
    @Override
    public void destroy() {
        super.destroy();
        try {
            cleanup();
        } catch (JMSException e) {
            log("Failed to clean up producer", e);
        }
    }
}

image.png
image.png

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Semoon 发表于 2023-4-26 15:42
端口被占用? https://blog.csdn.net/weixin_43705313/article/details/105517778

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
majunwang + 1 + 1 热心回复!

查看全部评分

 楼主| majunwang 发表于 2023-4-27 10:08
本帖最后由 majunwang 于 2023-4-27 10:10 编辑

已经解决了,有个hawtbuff-1.11.jar没有加进去,不太清楚这个包干嘛的
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 21:41

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表