如何使用 java 连接 MQ 并将数据发送到 MQ
How to connect and send data to MQ with java
我的机器中 运行 ActiveMQ (imqbrokerd.exe) 并获得了以下详细信息。我用
隐藏了我的机器名称
[#|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();
}
}
在这里提问...
一个。 JNDI_FACTORY、JMS_FACTORY、QUEUE、url 中的值应该是什么,它们表示什么?
b. url 中的 't3://' 是什么意思?这是协议吗?如果可以,主动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 队列详细信息的图像。我知道我使用的 URL 是错误的。你能帮我找到合适的吗?
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();
希望这对您有所帮助..
我已将 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);
JNDI_FACTORY 更像是您要用于连接的驱动程序,它们通常特定于供应商,在您的情况下是 weblogic 并且是预定义的。
JMS_FACTORY 是您已经在 Web 逻辑中为此类集成预定义的连接工厂。它负责管理与队列的连接。
QUEUE 也是您需要在 weblogic 管理控制台上预定义/设置的东西。它是对驻留在您事先设置的 weblogic 上的队列的引用。
t3 是您正在使用的连接类型,另一种选择可能是 iiop。 t3 它是一种更轻量级的连接类型。
我的机器中 运行 ActiveMQ (imqbrokerd.exe) 并获得了以下详细信息。我用
隐藏了我的机器名称[#|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();
}
}
在这里提问...
一个。 JNDI_FACTORY、JMS_FACTORY、QUEUE、url 中的值应该是什么,它们表示什么? b. url 中的 't3://' 是什么意思?这是协议吗?如果可以,主动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 队列详细信息的图像。我知道我使用的 URL 是错误的。你能帮我找到合适的吗?
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();
希望这对您有所帮助.. 我已将 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);
JNDI_FACTORY 更像是您要用于连接的驱动程序,它们通常特定于供应商,在您的情况下是 weblogic 并且是预定义的。
JMS_FACTORY 是您已经在 Web 逻辑中为此类集成预定义的连接工厂。它负责管理与队列的连接。
QUEUE 也是您需要在 weblogic 管理控制台上预定义/设置的东西。它是对驻留在您事先设置的 weblogic 上的队列的引用。
t3 是您正在使用的连接类型,另一种选择可能是 iiop。 t3 它是一种更轻量级的连接类型。