1 |
torben |
1271 |
package dk.thoerup.messagedriven; |
2 |
|
|
|
3 |
|
|
import java.io.IOException; |
4 |
|
|
|
5 |
|
|
import javax.jms.Connection; |
6 |
|
|
import javax.jms.ConnectionFactory; |
7 |
|
|
import javax.jms.MessageProducer; |
8 |
|
|
import javax.jms.Queue; |
9 |
|
|
import javax.jms.Session; |
10 |
|
|
import javax.jms.TextMessage; |
11 |
|
|
import javax.naming.InitialContext; |
12 |
|
|
import javax.servlet.ServletException; |
13 |
|
|
import javax.servlet.annotation.WebServlet; |
14 |
|
|
import javax.servlet.http.HttpServlet; |
15 |
|
|
import javax.servlet.http.HttpServletRequest; |
16 |
|
|
import javax.servlet.http.HttpServletResponse; |
17 |
|
|
|
18 |
|
|
|
19 |
|
|
@WebServlet(name = "ProducerServlet", urlPatterns = { "/ProducerServlet" }) |
20 |
|
|
public class ProducerServlet extends HttpServlet { |
21 |
|
|
private static final long serialVersionUID = 1L; |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* @see HttpServlet#HttpServlet() |
25 |
|
|
*/ |
26 |
|
|
public ProducerServlet() { |
27 |
|
|
super(); |
28 |
|
|
// TODO Auto-generated constructor stub |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
/* why doesn't these work ?? |
32 |
|
|
@Resource(mappedName = "jms/helloFactory") |
33 |
|
|
private static ConnectionFactory connectionFactory; |
34 |
|
|
|
35 |
|
|
@Resource(mappedName = "jms/hello") |
36 |
|
|
private static Queue queue; |
37 |
|
|
*/ |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
42 |
|
|
try { |
43 |
|
|
InitialContext ic = new InitialContext(); |
44 |
|
|
ConnectionFactory cf = (ConnectionFactory)ic.lookup("jms/helloFactory"); |
45 |
|
|
Connection connection = cf.createConnection(); |
46 |
|
|
Queue queue = (Queue)ic.lookup("jms/hello"); |
47 |
|
|
|
48 |
|
|
//Connection connection = connectionFactory.createConnection(); |
49 |
|
|
|
50 |
|
|
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
51 |
|
|
MessageProducer producer = session.createProducer(queue); |
52 |
|
|
TextMessage message = session.createTextMessage(); |
53 |
|
|
message.setStringProperty("name", "World!!"); |
54 |
|
|
message.setText("this is the body"); |
55 |
|
|
producer.send(message); |
56 |
|
|
response.getWriter().print("Message sent" ); |
57 |
|
|
} catch (Exception e) { |
58 |
|
|
response.getWriter().print("Error:" + e.getMessage() ); |
59 |
|
|
} |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
|
63 |
|
|
} |