我正在尝试向没有
SEND
权限的队列发送 2 条不同的消息。在设置 producer.setDeliveryMode(DeliveryMode.PERSISTENT)
的情况下,一切都很好(错误显示在控制台中),但是如果我设置值 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT)
那么就不会出现错误,并且消息不会发送到队列。我希望在两种情况下重现错误,或者这是经纪商的功能?
我的
broker.xml
:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<configuration xmlns="urn:activemq"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
<core xmlns="urn:activemq:core">
<name>localhost</name>
<bindings-directory>./data/messaging/bindings</bindings-directory>
<journal-directory>./data/messaging/journal</journal-directory>
<large-messages-directory>./data/messaging/largemessages</large-messages-directory>
<paging-directory>./data/messaging/paging</paging-directory>
<!-- true to expose ActiveMQ Artemis resources through JMX -->
<jmx-management-enabled>true</jmx-management-enabled>
<!-- Acceptors -->
<acceptors>
<acceptor name="netty">tcp://localhost:61616</acceptor>
</acceptors>
<!-- Other config -->
<security-settings>
<!--security for example queue-->
<security-setting match="exampleQueue">
<permission roles="amq" type="createDurableQueue"/>
<permission roles="amq" type="deleteDurableQueue"/>
<permission roles="amq" type="createNonDurableQueue"/>
<permission roles="amq" type="deleteNonDurableQueue"/>
<permission roles="amq" type="consume"/>
<permission roles="no" type="send"/>
<permission roles="amq" type="browse"/>
</security-setting>
<security-setting match="TestQueue">
<permission roles="amq" type="createDurableQueue"/>
<permission roles="amq" type="deleteDurableQueue"/>
<permission roles="amq" type="createNonDurableQueue"/>
<permission roles="amq" type="deleteNonDurableQueue"/>
<permission roles="amq" type="consume"/>
<permission roles="no" type="send"/>
<permission roles="amq" type="browse"/>
</security-setting>
<security-setting match="TestQueueSecond">
<permission roles="amq" type="createDurableQueue"/>
<permission roles="amq" type="deleteDurableQueue"/>
<permission roles="amq" type="createNonDurableQueue"/>
<permission roles="amq" type="deleteNonDurableQueue"/>
<permission roles="no" type="consume"/>
<permission roles="no" type="send"/>
</security-setting>
</security-settings>
<addresses>
<address name="exampleQueue">
<anycast>
<queue name="exampleQueue"/>
</anycast>
</address>
<address name="TestQueue">
<anycast>
<queue name="TestQueue"/>
</anycast>
</address>
<address name="TestQueueSecond">
<anycast>
<queue name="TestQueueSecond"/>
</anycast>
</address>
</addresses>
</core>
</configuration>
我的java课:
@RestController
public class Producer implements Serializable {
String url = "tcp://localhost:61616";
@GetMapping("/send")
public String sendMessage() throws JMSException {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
connectionFactory.setUser("test");
connectionFactory.setPassword("qwe123");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("exampleQueue");
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
Message message = session.createTextMessage("BodyTextMessage");
message.setStringProperty("TEST", "TEST");
producer.send(message);
String body = message.getBody(String.class);
connection.close();
return body;
}
这正是我向队列发送消息时的例子,但是并没有出现因为没有发送权限而无法发送消息的错误。
发送非持久消息时,ActiveMQ Artemis 的核心 JMS 客户端使用“即发即弃”语义,这意味着它不会等待确定发送是否成功;它只是将控制权返回给发送应用程序。这是基于非持久消息本质上易失性这一事实的性能优化。换句话说,由于非持久性消息无法在代理重新启动后继续存在,这意味着它们的丢失是可以接受的。
您可以通过在连接 URL 上设置
blockOnNonDurableSend=true
来更改这些语义,例如:
String url = "tcp://localhost:61616?blockOnNonDurableSend=true";
请参阅文档了解更多信息。