lundi 9 mars 2015

how do I invoke appclient from within Java?



This SSCCE works so far as it goes. How do I incorporate it into a larger project? For example, how would a Swing GUI project use this producer to send a message? Or, even just another CLI class which instantiates messages.


Keeping in mind that it's invoked with Glassfish appclient:



appclient -client NetBeansProjects/JMSPTPProducer/dist/JMSPTPProducer.jar


I know that JAR's or libraries can be added so that appclient isn't required to execute, but what are the alternatives?



package net.ensode.glassfishbook;

import java.util.Date;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSProducer;
import javax.jms.Queue;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JndiSender {

@Resource(mappedName = "jms/CrmConnectionFactory")
// @Resource(mappedName = "jms/GlassFishBookConnectionFactory")
private static ConnectionFactory connectionFactory;

@Resource(mappedName = "jms/CrmQueue")
// @Resource(mappedName = "jms/GlassFishBookQueue")
private static Queue queue;

public void produceMessages() throws NamingException {
Context jndiContext = new InitialContext();
System.out.println(jndiContext.getEnvironment().toString());
JMSContext jmsContext = connectionFactory.createContext();
JMSProducer jmsProducer = jmsContext.createProducer();

// ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory");
Destination queue = (Destination) jndiContext.lookup("jms/CrmQueue");

try (JMSContext context = connectionFactory.createContext()) {
context.createProducer().send(queue, "Text message sent at " + new Date());
}
}

public static void main(String[] args) throws NamingException {
new JndiSender().produceMessages();
}
}


see also:


http://ift.tt/1HqiZIq




Aucun commentaire:

Enregistrer un commentaire