我刚刚开始使用Java ee 7,这里有一些我不明白它是如何神奇地工作的东西。
我遵循安东尼奥·贡卡尔维斯的书“开始Java EE 7”中的例子。我设法编译和部署了第13章(关于JMS)的代码,没有任何问题。消息按预期发送和接收,但这让我感到困惑。
源代码由一个消费者类、一个生产者类、一个POJO和一个MDB类组成。
下面是消费者:
public class OrderConsumer {
public static void main(String[] args) throws NamingException {
// Gets the JNDI context
Context jndiContext = new InitialContext();
// Looks up the administered objects
ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory");
Destination topic = (Destination) jndiContext.lookup("jms/javaee7/Topic");
// Loops to receive the messages
System.out.println("\nInfinite loop. Waiting for a message...");
try (JMSContext jmsContext = connectionFactory.createContext()) {
while (true) {
OrderDTO order = jmsContext.createConsumer(topic).receiveBody(OrderDTO.class);
System.out.println("Order received: " + order);
}
}
}
}制片人:
public class OrderProducer {
public static void main(String[] args) throws NamingException {
if (args.length != 1) {
System.out.println("usage : enter an amount");
System.exit(0);
}
System.out.println("Sending message with amount = " + args[0]);
// Creates an orderDto with a total amount parameter
Float totalAmount = Float.valueOf(args[0]);
OrderDTO order = new OrderDTO(1234l, new Date(), "Serge Gainsbourg", totalAmount);
// Gets the JNDI context
Context jndiContext = new InitialContext();
// Looks up the administered objects
ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory");
Destination topic = (Destination) jndiContext.lookup("jms/javaee7/Topic");
try (JMSContext jmsContext = connectionFactory.createContext()) {
// Sends an object message to the topic
jmsContext.createProducer().setProperty("orderAmount", totalAmount).send(topic, order);
System.out.println("\nOrder sent : " + order.toString());
}
}
}MDB:
@MessageDriven(mappedName = "jms/javaee7/Topic", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "orderAmount > 1000")
})
public class ExpensiveOrderMDB implements MessageListener {
public void onMessage(Message message) {
try {
OrderDTO order = message.getBody(OrderDTO.class);
System.out.println("Expensive order received: " + order.toString());
} catch (JMSException e) {
e.printStackTrace();
}
}
}封装在实现Serializable接口的POJO对象中的msg内容
ExpensiveOrderMDB和POJO被打包在一个.jar文件中,并部署在本地运行的glassfish服务器中。连接和去站点资源由asadmin创建。
问题是:消费者和生产者如何知道本地glassfish服务器上的连接和目的地可供其建立连接和发送/接收msg?(创建连接和目标的代码行没有说明本地glassfish服务器)
发布于 2013-11-29 20:19:41
可能存在一个jndi.properties文件,其中定义了与glassfish的连接
https://stackoverflow.com/questions/20261359
复制相似问题