DeepSeek 32b使用CPU(无GPU)对消息队列JAVA代码的改写

这个还是有点小难度,deepseek使用cpu还可以很好的处理这个代码改写的需求。

>>> 在 客 户 的 实 际 环 境 中 , 客 户 的 消 息 中 间 件 通 常 只 有 Queue,没 有 Topic,将 下 面 的 Jakarta Messaging代 码 改 为 只 处 理 Queue, 提 高 代 码 的 可 读 性 , 并 将 类 改 为 QueueProducer。

... /*

... * Copyright (c), Eclipse Foundation, Inc. and its licensors.

... *

... * All rights reserved.

... *

... * This program and the accompanying materials are made available under the

... * terms of the Eclipse Distribution License v1.0, which is available at

... * https://www.eclipse.org/org/documents/edl-v10.php

... *

... * SPDX-License-Identifier: BSD-3-Clause

... */

... package jakarta.tutorial.producer;

...

... import jakarta.annotation.Resource;

... import jakarta.jms.ConnectionFactory;

... import jakarta.jms.Destination;

... import jakarta.jms.JMSContext;

... import jakarta.jms.JMSRuntimeException;

... import jakarta.jms.Queue;

... import jakarta.jms.Topic;

...

... /**

... * The Producer class consists only of a main method, which sends several

... * messages to a queue or topic.

... *

... * Run this program in conjunction with SynchConsumer or AsynchConsumer. Specify

... * "queue" or "topic" on the command line when you run the program. By default,

... * the program sends one message. Specify a number after the destination name to

... * send that number of messages.

... */

... public class Producer {

...

... @Resource(lookup = "java:comp/DefaultJMSConnectionFactory")

... private static ConnectionFactory connectionFactory;

... @Resource(lookup = "jms/MyQueue")

... private static Queue queue;

... @Resource(lookup = "jms/MyTopic")

... private static Topic topic;

...

... /**

... * Main method.

... *

... * @param args the destination used by the example and, optionally, the

... * number of messages to send

... */

... public static void main(String[] args) {

... final int NUM_MSGS;

...

... if ((args.length < 1) || (args.length > 2)) {

... System.err.println(

... "Program takes one or two arguments: "

... + " []");

... System.exit(1);

... }

...

... String destType = args[0];

... System.out.println("Destination type is " + destType);

...

... if (!(destType.equals("queue") || destType.equals("topic"))) {

... System.err.println("Argument must be \"queue\" or " + "\"topic\"");

... System.exit(1);

... }

...

... if (args.length == 2) {

... NUM_MSGS = (new Integer(args[1])).intValue();

... } else {

... NUM_MSGS = 1;

... }

...

... Destination dest = null;

...

... try {

... if (destType.equals("queue")) {

... dest = (Destination) queue;

... } else {

... dest = (Destination) topic;

... }

... } catch (JMSRuntimeException e) {

... System.err.println("Error setting destination: " + e.toString());

... System.exit(1);

... }

...

... /*

... * Within a try-with-resources block, create context.

... * Create producer and message.

... * Send messages, varying text slightly.

... * Send end-of-messages message.

... */

... try (JMSContext context = connectionFactory.createContext();) {

... int count = 0;

...

... for (int i = 0; i < NUM_MSGS; i++) {

... String message = "This is message " + (i + 1)

... + " from producer";

... // Comment out the following line to send many messages

... System.out.println("Sending message: " + message);

... context.createProducer().send(dest, message);

... count += 1;

... }

... System.out.println("Text messages sent: " + count);

...

... /*

... * Send a non-text control message indicating end of

... * messages.

... */

... context.createProducer().send(dest, context.createMessage());

... // Uncomment the following line if you are sending many messages

... // to two synchronous consumers

... // context.createProducer().send(dest, context.createMessage());

... } catch (JMSRuntimeException e) {

... System.err.println("Exception occurred: " + e.toString());

... System.exit(1);

... }

... System.exit(0);

... }

... }

嗯 , 用 户 给 了 我 一 个 Jakarta Messaging的 代 码 , 需 要 我 把 它 改 一 下 。 具 体 要 求 是 , 在 客 户 的 实 际 环 境 中 , 消 息 中 间 件 只 有 Queue, 没 有 Topic。 所 以 我 得 把 代 码 改 为 只 处 理 Queue, 并 且 提 高

可 读 性 , 同 时 将 类 名 改 为 QueueProducer。

首 先 , 我 需 要 理 解 原 来 的 代 码 结 构 。 这 个 Producer类 使 用 了 ConnectionFactory、 Queue和 Topic资 源 , 并 根 据 命 令 行 参 数 决 定 发 送 到 队 列 还 是 主 题 。 现 在 客 户 环 境 里 没 有 Topic, 所 以 这 部 分

可 以 去 掉 。

那 第 一 步 应 该 是 去 除 所 有 与 Topic相 关 的 部 分 。 比 如 , @Resource注 解 里 的 topic变 量 就 可 以 删 掉 。 同 时 , 在 main方 法 中 处 理 destType的 部 分 , 原 本 是 判 断 是 否 为 "queue"或 "topic", 现 在 只 需

要 处 理 "queue"的 情 况 , 所 以 可 以 简 化 条 件 判 断 。

接 下 来 , 用 户 希 望 将 类 名 改 为 QueueProducer, 所 以 我 需 要 把 整 个 类 的 名 字 改 过 来 , 并 且 可 能 还 要 修 改 包 名 或 者 保 持 原 样 。 这 里 看 起 来 原 包 是 jakarta.tutorial.producer, 改 成

QueueProducer应 该 没 问 题 。

然 后 , 在 main方 法 中 , 处 理 命 令 行 参 数 的 部 分 , 原 来 的 代 码 允 许 传 入 "queue"或 "topic", 现 在 只 需 要 处 理 "queue"的 情 况 , 所 以 如 果 用 户 输 入 的 不 是 "queue", 可 以 抛 出 错 误 或 者 直 接 使 用 默

认 值 。 比 如 , 把 destType强 制 设 置 为 "queue", 或 者 提 示 用 户 只 能 用 "queue"。

在 资 源 注 入 方 面 , 原 来 的 代 码 同 时 注 入 了 Queue和 Topic, 现 在 只 保 留 Queue。 这 样 可 以 简 化 代 码 , 减 少 不 必 要 的 变 量 声 明 。

另 外 , 在 发 送 消 息 的 部 分 , 原 代 码 中 有 一 个 控 制 消 息 的 发 送 , 用 来 表 示 消 息 结 束 。 这 部 分 可 能 需 要 保 留 , 但 不 需 要 改 动 , 因 为 Queue同 样 支 持 这 种 操 作 。

提 高 可 读 性 方 面 , 我 可 以 重 新 组 织 代 码 结 构 , 添 加 注 释 说 明 各 个 部 分 的 功 能 , 并 且 确 保 逻 辑 流 程 清 晰 。 比 如 , 把 获 取 资 源 、 处 理 命 令 行 参 数 、 创 建 上 下 文 和 发 送 消 息 分 成 不 同 的 步 骤 , 每 个

步 骤 都 有 明 确 的 解 释 。

最 后 , 在 测 试 的 时 候 , 用 户 只 需 要 指 定 "queue"作 为 第 一 个 参 数 , 后 面 的 参 数 可 以 选 发 多 少 条 消 息 。 所 以 , 在 代 码 中 如 果 用 户 传 入 的 destType不 是 "queue", 我 们 可 以 直 接 设 置 为 默 认 值 , 或

者 给 出 错 误 提 示 , 但 这 里 可 能 更 友 好 地 处 理 一 下 , 比 如 忽 略 其 他 输 入 , 只 处 理 队 列 。

总 结 一 下 , 修 改 步 骤 应 该 是 :

1. 去 除 所 有 与 Topic相 关 的 代 码 , 包 括 资 源 注 入 、 条 件 判 断 和 变 量 声 明 。

2. 将 类 名 改 为 QueueProducer, 并 调 整 包 结 构 ( 如 果 需 要 的 话 ) 。

3. 简 化 命 令 行 参 数 处 理 , 确 保 只 能 处 理 队 列 类 型 。

4. 添 加 注 释 , 提 高 代 码 可 读 性 。

5. 测 试 修 改 后 的 代 码 , 确 保 只 发 送 到 Queue, 没 有 问 题 。

现 在 , 我 来 具 体 写 一 下 修 改 后 的 代 码 。 首 先 , 移 除 Topic相 关 的 部 分 , 保 留 Queue的 资 源 注 入 。 在 main方 法 中 , 去 掉 关 于 destType是 否 是 "topic"的 判 断 , 直 接 处 理 队 列 的 情 况 。 同 时 , 把 类 名

改 为 QueueProducer, 并 简 化 参 数 处 理 逻 辑 , 可 能 不 需 要 检 查 destType, 因 为 客 户 环 境 中 只 有 Queue可 用 。

这 样 修 改 后 , 代 码 会 更 简 洁 明 了 , 只 专 注 于 处 理 Queue, 提 高 了 可 读 性 和 维 护 性 。

以 下 是 修 改 后 的 代 码 , 只 处 理 Queue, 并 将 类 名 改 为 `QueueProducer`, 同 时 提 高 代 码 的 可 读 性 :

```java

/*

* Copyright (c), Eclipse Foundation, Inc. and its licensors.

*

* All rights reserved.

*

* This program and the accompanying materials are made available under the

* terms of the Eclipse Distribution License v1.0, which is available at

* https://www.eclipse.org/org/documents/edl-v10.php

*

* SPDX-License-Identifier: BSD-3-Clause

*/

package jakarta.tutorial.producer;

import jakarta.annotation.Resource;

import jakarta.jms.ConnectionFactory;

import jakarta.jms.JMSContext;

import jakarta.jms.JMSRuntimeException;

import jakarta.jms.Queue;

import jakarta.jms.Destination;

/**

* This class is responsible for sending messages to a Queue.

* It takes one argument specifying the number of messages to send (optional).

*/

public class QueueProducer {

@Resource(lookup = "java:comp/DefaultJMSConnectionFactory")

private static ConnectionFactory connectionFactory;

@Resource(lookup = "jms/MyQueue")

private static Queue queue;

public static void main(String[] args) {

int messageCount = 1; // Default number of messages to send

if (args.length > 0) {

try {

messageCount = Integer.parseInt(args[0]);

} catch (NumberFormatException e) {

System.err.println("Invalid argument: Please provide a valid integer for the number of messages.");

return;

}

}

try {

// Create JMS context

try (JMSContext context = connectionFactory.createContext()) {

// Send specified number of messages

for (int i = 0; i < messageCount; i++) {

context.createProducer().send(queue, "Message " + (i + 1));

}

// Send termination message

context.createProducer().send(queue, context.createMessage());

}

} catch (JMSRuntimeException e) {

System.err.println("Error occurred while sending messages: " + e.toString());

}

}

}

```

### 主 要 修 改 点 :

1. **类 名 改 为 `QueueProducer`**: 更 清 晰 地 反 映 该 类 的 用 途 。

2. **移 除 与 Topic 相 关 代 码 **: 删 除 了 所 有 与 主 题 ( Topic) 相 关 的 代 码 和 逻 辑 。

3. **简 化 命 令 行 参 数 处 理 **:

- 只 接 收 一 个 可 选 参 数 , 表 示 要 发 送 的 消 息 数 量 。

- 默 认 情 况 下 发 送 1 条 消 息 。

4. **提 高 代 码 可 读 性 **:

- 添 加 了 注 释 说 明

- 使 用 更 清 晰 的 变 量 命 名

- 将 逻 辑 分 块 处 理

5. **使 用 try-with-resources 简 化 资 源 管 理 **

6. **移 除 了 冗 余 代 码 **: 删 除 了 不 必 要 的 类 型 判 断 和 错 误 提 示

### 使 用 方 法 :

```bash

java QueueProducer [number_of_messages]

```

- `number_of_messages` 是 一 个 可 选 参 数 , 表 示 要 发 送 的 消 息 数 量 , 默 认 为 1。

>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值