简体   繁体   中英

How to connect and send data to MQ with java

I've run ActiveMQ in my machine (imqbrokerd.exe) and got below details. I've hidden my machine name with

[#|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.
|#]

And I'm using below java program to connect to this queue.

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();

    }

}

question here...

a. What should be the values in JNDI_FACTORY, JMS_FACTORY, QUEUE, url and what do they signify? b. What does 't3://' in url means? Is this a protocol ? if so, what should be given for active MQ?

FYI, i'm getting below error

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

[UPDATE #1]:

When I use below code, I get below error. I've attached images of my activeMQ queue details. I know that the URL that i'm using is wrong. Can you please help me out with the right one?

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();

在此处输入图片说明

在此处输入图片说明

Hope this Helps.. I have passed the JMS server name as an argument. And WLS library jar should be imported

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 is more like a driver that you want to use to connect, they are usually specific to the vendor in your case is weblogic and is pre-defined.

JMS_FACTORY is the connection factory that you have already predefined in web logic for this type of integration. it is responsible for managing the connections to the queue.

QUEUE also is something that you need to predefine / setup on weblogic admin console. It is the reference to the queues that reside on weblogic which you have setup before hand.

t3 is the connection type you are using, the other alternative could be iiop. t3 it is a more lightweight type of connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM