gpt4 book ai didi

java - 如何用java连接MQ并发送数据

转载 作者:太空宇宙 更新时间:2023-11-04 13:23:06 25 4
gpt4 key购买 nike

我已在我的计算机 (imqbrokerd.exe) 中运行 ActiveMQ 并获得以下详细信息。我用

隐藏了我的机器名称
[#|2015-10-01T19:16:06.788+0530|WARNING|5.1|imq.log.Logger|_ThreadID=1;_ThreadNa
me=main;|[S2004]: Log output channel com.sun.messaging.jmq.util.log.SysLogHandle
r is disabled: no imqutil in java.library.path|#]


[#|2015-10-01T19:16:06.804+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|
================================================================================

Message Queue 5.1
Oracle
Version: 5.1 (Build 9-b)
Compile: July 29 2014 1229

Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
================================================================================

Java Runtime: 1.7.0_40 Oracle Corporation C:\Program Files (x86)\Java\jre7
|#]


[#|2015-10-01T19:16:06.819+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;| IMQ_HOME=C:\MessageQueue5.1\mq
|#]


[#|2015-10-01T19:16:06.819+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|IMQ_VARHOME=C:\MessageQueue5.1\var\mq
|#]


[#|2015-10-01T19:16:06.819+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|Windows 7 6.1 x86 <MachineName> (4 cpu)

|#]


[#|2015-10-01T19:16:06.835+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|Java Heap Size: max=190080k, current=15872k
|#]


[#|2015-10-01T19:16:06.835+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|Arguments:
|#]


[#|2015-10-01T19:16:06.850+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|[B1060]: Loading persistent data...
|#]


[#|2015-10-01T19:16:06.866+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|Using built-in file-based persistent store: C:\MessageQueue5.1\var\mq\ins
tances\imqbroker\
|#]


[#|2015-10-01T19:16:07.194+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|[B1270]: Processing messages from transaction log file...
|#]


[#|2015-10-01T19:16:07.396+0530|FORCE|5.1|imq.log.Logger|_ThreadID=1;_ThreadName
=main;|[B1039]: Broker "imqbroker@<MachineName>:7676"
ready.
|#]

我正在使用下面的java程序连接到这个队列。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueSendLinear {


public static void main(String args[]) throws JMSException, NamingException {
// Defines the JNDI context factory.
final String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

// Defines the JMS context factory.
final String JMS_FACTORY="jms/TestConnectionFactory";

// Defines the queue.
final String QUEUE="jms/TestJMSQueue";

QueueConnectionFactory qconFactory;
QueueConnection qcon;
QueueSession qsession;
QueueSender qsender;
Queue queue;
TextMessage msg;
String xml = "Sample XML comes here!! ";

String url = "t3://<MachineName>:7676";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
InitialContext ic = new InitialContext(env);

qconFactory = (QueueConnectionFactory) ic.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ic.lookup(QUEUE);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
msg.setText(xml);
qsender.send(msg);
qsender.close();
qsession.close();
qcon.close();

}

}

这里有问题...

a. JNDI_FACTORY、JMS_FACTORY、QUEUE、url 中的值应该是什么?它们代表什么?b. url 中的“t3://”是什么意思?这是一个协议(protocol)吗?如果是这样,主动 MQ 应该给出什么?

仅供引用,我遇到了以下错误

Oct 01, 2015 7:20:58 PM com.sun.corba.se.impl.naming.namingutil.CorbalocURL badAddress
WARNING: "IOP00110603: (BAD_PARAM) Bad host address in -ORBInitDef"
org.omg.CORBA.BAD_PARAM: vmcid: SUN minor code: 603 completed: No

[更新#1]:

当我使用下面的代码时,出现以下错误。我已附上我的 activeMQ 队列详细信息的图像。我知道我使用的网址是错误的。您能帮我找到合适的吗?

Exception in thread "main" javax.naming.NamingException: Couldn't connect to the specified host :  [Root exception is org.omg.CORBA.COMM_FAILURE:   vmcid: SUN  minor code: 208 completed: Maybe]
at weblogic.corba.j2ee.naming.Utils.wrapNamingException(Utils.java:83)
at weblogic.corba.j2ee.naming.ORBHelper.getORBReferenceWithRetry(ORBHelper.java:656)

final String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
final String JMS_FACTORY="jms/?";
final String QUEUE = "mq.sys.dmq";
QueueConnectionFactory qconFactory;
QueueConnection qcon;
QueueSession qsession;
QueueSender qsender;
Queue queue;
TextMessage msg;
String xml = "Sample XML comes here!! ";
String url = "t3://localhost:51010";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
InitialContext ic = new InitialContext(env);
qconFactory = (QueueConnectionFactory) ic.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ic.lookup(QUEUE);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();

enter image description here

enter image description here

最佳答案

希望这有帮助..我已将 JMS 服务器名称作为参数传递。并且应该导入WLS库jar

private static final String CONNECTION_FACTORY_NAME ="connection factory name goes here";  

private static final String TOPIC_NAME = "Topic Name goes here";


private static final String SERVER_URL_PREFIX = "t3://";
private static final String SERVER_URL_SUFFIX = ".url.com:port";
private static final String USER = "";
private static final String PASSWORD = "";

private static final String LOCAL_DIRECTORY = "C:\\tmp\\poslog\\";


public static void main(String args[]) throws JMSException,
NamingException, IOException, InterruptedException {

System.out.println("start" + new Date());
// INITIALIZE
System.out.println("creating context for " + args[0]);
Hashtable<String, String> properties = new Hashtable<String, String>();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, SERVER_URL_PREFIX + args[0] + SERVER_URL_SUFFIX);
//properties.put(Context.SECURITY_PRINCIPAL, USER);
//properties.put(Context.SECURITY_CREDENTIALS, PASSWORD);
InitialContext ctx = new InitialContext(properties);
TopicConnectionFactory connectionFactory = (TopicConnectionFactory) ctx
.lookup(CONNECTION_FACTORY_NAME);
TopicConnection connection = connectionFactory.createTopicConnection();
TopicSession session = connection.createTopicSession(false, 0);
Topic topic = (Topic) ctx.lookup(TOPIC_NAME);
TopicPublisher sender = session.createPublisher(topic);

关于java - 如何用java连接MQ并发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32889473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com