我们正在从weblogic jms转到activemq。我们将所有activemq所需的jar文件放入应用程序的类路径中,并创建了一个属性文件,其中包含activemq url和connectionfactory详细信息,如下所示:
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url=tcp://localhost:61616
connectionFactoryNames=TestTopicConnectionFactory
topic.mq/TestTopic=TestTopic
During server startup we are loading this properties file as below and starting the MQ broker srevice.
public TestServlet extends HttpServlet {
private MQStartup mqStartup = null;
@Override
public void init(ServletConfig config)
throws ServletException {
super.init(config);
mqStartup = new MQStartup();
mqStartup.startBrokerService();
}
}
public class MQStartup {
private final String bindAddress;
private BrokerService broker = new BrokerService();
public MQStartup(){
MQContext context = new MQContext();
context.loadJndiProperties();
bindAddress = ActiveMQContext.getProperty("java.naming.provider.url");
}
public void startBrokerService() {
broker.addConnector(bindAddress);
broker.start();
}
public void stopBrokerService() {
try {
broker.stop();
}catch (Exception e) {
logger.error("Unable to stop the MQ Broker service " +e);
}
}
}
public class MQContext {
private static final Object singletonLock = new Object();
static Context context = null;
static Properties properties = null;
private boolean loaded = false;
public void loadJndiProperties() {
try {
if (!loaded) {
synchronized (singletonLock) {
URL url = getClass().getClassLoader().getResource("jda-activemq-jndi.properties");
properties = new Properties();
properties.load(url.openStream());
context = new InitialContext(properties);
loaded = true;
}
}
} catch (IOException | NamingException ie) {
logger.error("Failed to load apachemq jndi", ie);
}
}
public static Context getContext() {
return context;
}
public static Properties getProperties() {
return properties;
}
public static String getProperty(String propertyName) {
return properties.getProperty(propertyName);
}
}
在用于生成消息的实现类中,我们将在下面获得mq connectionfactory的内容:
TopicConnectionFactory factory = (TopicConnectionFactory) MQContext.getContext().lookup("<topicFactoryName>");
this.topicConnection = factory.createTopicConnection();
this.topicConnection.start();
,然后创建主题会话并使用Publisher.publish(“ message”);
但是这里的问题是我们在集群中有3个应用程序实例,而对于这3个实例,我们有3个嵌入式activemq代理服务。
当我生成一条消息并在instance1中发送时(假设Web服务器正在击中instance1),other2实例不会使用此消息。
我在google上搜索了一些networkconnector配置,可以为您提供帮助。但是问题是我们正在使用JNDI,MQ不是单独的安装。
是否可以使用嵌入式MQ和JNDI来完成此操作。
注意:在weblogic中,我们有 UniformDistributedTopic 类型的主题,可以帮助实现上述目标,但是在MQ中,似乎没有这种主题类型。