Java Reference
In-Depth Information
import javax.jms.JMSConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.Queue;
@Named
@RequestScoped
public class JmsMessageController {
@Inject
private JmsMessageModel jmsMessageModel;
@Resource(mappedName = "jms/myQueue")
private Queue myQueue;
@Inject
@JMSConnectionFactory("java:comp/DefaultJMSConnectionFactory")
private JMSContext context;
public String sendMsg() {
sendJMSMessageToMyQueue(jmsMessageModel.getMsgText());
return "confirmation";
}
private void sendJMSMessageToMyQueue(String messageData) {
context.createProducer().send(myQueue, messageData);
}
}
As we can see, all we had to do was to inject an instance of the JmsMessageModel
class we developed earlier and add a simple method that will invoke the generated
sendJMSMessageToMyQueue() method, passing the message text as a parameter.
Next, we need to modify the generated index.xhtml file to add a form binding the
msgText variable of JmsMessageModel to a text field and a command button that
invokes the sendMsg() method when it is clicked. The code is as follows:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Send JMS Message</title>
</h:head>
<h:body>
 
Search WWH ::




Custom Search