git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Artemis (JBoss EAP 7) to HornetQ (EAP 6) elaboration on RedHat example


This is the code I'm trying - currently just trying to run it from eclipse -
but it's also technically an EJB being deployed in an EAR (not really
important at the moment)

I'm also going to try and attach it for easier reading like in notepad++
TestSender2.java
<http://activemq.2283324.n4.nabble.com/file/t379195/TestSender2.java>  

*I tried to comment the changes made for Artemis per the Red Hat post/link:*
package net.myco.utility;

import java.io.StringReader;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Stateless;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

@Startup
@Singleton
//@Stateless
public class TestSender2 {

    /**
     * Default constructor. 
     */
    public TestSender2() {
        // TODO Auto-generated constructor stub
    }
    
    public static void main(String [] args){
    	System.out.println("Trying to Send message");
    	sendJMSMessage("myself", "<testXML>Hello IAT</testXML>");
    	System.out.println("Sent message");
    }
    
    /*
     * This is just for testing in JBoss when the app is first deployed
     */
    @PostConstruct
    public void autoSendMessage(){
    	System.out.println("Trying Auto Send message");
    	sendJMSMessage("myself", "<testXML>Hello IAT Auto</testXML>");
    	System.out.println("Auto sent message");
    }
    
    public static boolean sendJMSMessage(String caller, String xml) {
		System.out.println("InterceptorJMSProducer:->sendJMSMessage:->For " +
caller + " with:\r\n" + xml);
		
		/////////////////////
		QueueSender sender = null;
        QueueSession session = null;
        QueueConnectionFactory factory = null;
        QueueConnection queueConnection = null;
        Context ctx = null;
        Queue queue1 = null;
        try {

            String jndiProps =
                   
"java.naming.factory.url.pkgs=org.jboss.ejb.client.naming\n"
                    //J.G. EAP 7 test change, this works if JBoss EAP 6 jars
are included
            		//+
"java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory\n"
            		//Artemis change
            		+
"java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory\n"
            		//Below works fine in standalone java app with no Artemis but
JBoss 6 libraries
            		//+ "java.naming.provider.url=remote://10.140.40.157:30202\n"
            		//this may not be needed - but Artemis change
            		+ "java.naming.provider.url=tcp://10.140.40.157:30202\n"
//according to a post, you dont use the naming/remoting you use the
messaging port
            		//Artemis change - addition
            		+
"connectionFactory.jms/ConnectionFactory=tcp://10.140.40.157:30206\n"
            		//Artemis change - addition
            		+
"jms.NotificationQueue=java:/com/vendor/samp/imp/mycompany/ejb/NotificationQueue\n"
            		+ "java.naming.security.principal=samp.abcd1\n"
                    + "java.naming.security.credentials=PW_abcd1\n"
                    + "jboss.naming.client.ejb.context=true\n"
                    //Artemis change - addition
                    +
"protocolManagerFactoryStr=org.apache.activemq.artemis.core.protocol.hornetq.client.HornetQClientProtocolManagerFactory\n"
                    +
"jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=true\n"
                    +
"jboss.naming.client.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=JBOSS-LOCAL-USER\n"
                    +
"jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false\n"
                    +
"jboss.naming.client.connect.options.org.xnio.Options.SSL_STARTTLS=true\n"
                    +
"jboss.naming.client.remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=true";

            Properties props = new Properties();
            props.load(new StringReader(jndiProps));
            
            /*
             * TODO - investigate, research or post question to jboss forums
as this throws an exception of " EJBCLIENT000405: An EJB client context is
already 
             * registered for EJB client context identifier [Named EJB
client context identifier: RemoteNamingEJBClientContext"
             * 
             * For now add try/catch as it doesnt seem fatal
             */
            try{
            	ctx = new InitialContext(props);
            }catch(Exception e){
            
System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Caught
exception: " + e.getMessage());
            	e.printStackTrace();
            }
           
System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Created
initial context");
            
           
System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Creating
Factory");
			if(ctx != null){
	            //Artemis change - Red Hat says you shouldnt need the below if
not using administered objects, e.g. jndi
	            //factory = (QueueConnectionFactory)
ctx.lookup("java:/System/Vendor/ApplicationType/Management/Application/6-1;2-0;ZZZ/Comp/QueueConnectionFactory");
	            factory = (QueueConnectionFactory)
ctx.lookup("jms/ConnectionFactory");
	           
System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Created
Factory");
	            
	           
System.out.println("InterceptorJMSProducer:->sendJMSMessage:->looking up
queue");
				//Artemis change
	            //queue1 = (Queue)
ctx.lookup("java:/com/vendor/samp/imp/mycompany/ejb/NotificationQueue");
	            queue1 = (Queue) ctx.lookup("jms/NotificationQueue");
				System.out.println("InterceptorJMSProducer:->sendJMSMessage:->looked up
queue");				
				
				System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Creating
queue connection");
				queueConnection = factory.createQueueConnection("samp.csra1",
"PW_csra1");
				System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Created
queue connection");
				
			
System.out.println("InterceptorJMSSigmaProducer:->sendJMSMessage:->Creating
queue session");
				session = queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
				System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Created
queue session");
	
	            queueConnection.start();
				
	           
System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Started the
queue connection");
	
				sender = session.createSender(queue1);
	
				System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Created
Sender");
				
				// create and send the message
	            TextMessage objectMessage = session.createTextMessage(xml);
	            sender.send(objectMessage);
	           
System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Sent
Message");
            }else{
            	System.out.println("Context is null");
            }
        } catch (NamingException e) {
        
System.out.println("InterceptorJMSProducer:->sendJMSMessage:->NamingException
" + e.getMessage() + " - Could not send message");
            e.printStackTrace();
        } catch (Exception e) {
        
System.out.println("InterceptorJMSSigmaProducer:->sendJMSMessage:->Exception
caught in try: " + e.getMessage());
            e.printStackTrace();
        } finally {
        	try{
        		if (sender != null) { sender.close(); } 
        		if (session !=null) { session.close(); } 
        		if (queueConnection != null){ queueConnection.close(); } 
        		if (ctx != null) { ctx = null; }
        	}catch(Exception e){
        	
System.out.println("InterceptorJMSProducer:->sendJMSMessage:->Exception
caught in Finally: " + e.getMessage());
        	}
        }

		return false;
	}
    
    
}

*The console output is:*
Trying to Send message
InterceptorJMSProducer:->sendJMSMessage:->For myself with:
<testXML>Hello IAT</testXML>
InterceptorJMSProducer:->sendJMSMessage:->Created initial context
InterceptorJMSProducer:->sendJMSMessage:->Creating Factory
InterceptorJMSProducer:->sendJMSMessage:->Created Factory
InterceptorJMSProducer:->sendJMSMessage:->looking up queue
InterceptorJMSProducer:->sendJMSMessage:->NamingException NotificationQueue
- Could not send message
javax.naming.NameNotFoundException: NotificationQueue
Sent message
	at
org.apache.activemq.artemis.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:241)
	at
org.apache.activemq.artemis.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:277)
	at
org.apache.activemq.artemis.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:245)
	at javax.naming.InitialContext.lookup(InitialContext.java:417)
	at net.myco.utility.TestSender.sendJMSMessage(TestSender.java:120)
	at net.myco.utility.TestSender.main(TestSender.java:39)


I also have -Djavax.net.debug=all on, and *nothing* is being output so I'm
pretty sure it's not even connecting to the remote server.

Of, note, my classpath includes:
<http://activemq.2283324.n4.nabble.com/file/t379195/Artemis.png> 

And my apologies, the formatting (indention) got a little messed up.






--
Sent from: http://activemq.2283324.n4.nabble.com/ActiveMQ-User-f2341805.html