JMS消息服务器和Hibernate、Spring的集成开发
来源:互联网 发布:兼职软件 编辑:程序博客网 时间:2024/05/18 00:09
工作需要使用JMS实现输入框的消息管理,就花两天时间学习了一下,现在整理如下,以备不时之需。
JMS用来实现消息管理,ActiveMQ是用来实现JMS框架。
开发JMS的步骤:
1 安装ActiveMQ服务器(在这里),并运行
2 创建Web项目,引用jar包
activemq-all-5.5.0.jar
javax.jms.jar
3 建立发送消息的源代码,将消息发送到ActiveMQ服务器上
public class QueueSend { //queue方式的jms连接 QueueConnection queueConection; //queue回话 QueueSession queueSession; //queue消息发送者 QueueSender queueSender; Queue queue; public QueueSend(){} public QueueSend(String factoryJNDI,String topicJNDI){ //连接jms Provider的环境参数 Hashtable<String, String> props = new Hashtable<String,String>(); String INITIAL_CONTEXT_FACTORY = ""; props.put(INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); try { new InitialContext(props); Context ctx = new InitialContext(); Context context = ctx; String connectionFactoryName = "xxx"; QueueConnectionFactory queueFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); //创建连接 queueConection = queueFactory.createQueueConnection(); int AUTO_ACKNOWLEDGE = 1; queueSession = queueConection.createQueueSession(false, AUTO_ACKNOWLEDGE); queue = new ActiveMQQueue("jason.queue2"); //创建队列消息的发送者 queueSender = queueSession.createSender(queue); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void send(String msg) throws JMSException{ TextMessage message = queueSession.createTextMessage(); message.setText(msg); queueSender.send(queue,message); } public void close() throws JMSException{ queueSession.close(); queueConection.close(); } public static void main(String[] args) throws JMSException { QueueSend queue = new QueueSend("ConnectionFactory", "queue/testQueue"); for(int i = 19; i<21 ;i++){ String msg = "Hello World no "+i; System.out.println("Hello Queue Publishing message:" + msg); queue.send(msg); } } }
4 建立接受消息的源代码,把消息从ActiveMQ服务器上接受下来
public class QueueConsumer { public static void main(String[] args) { QueueConsumer rs = new QueueConsumer(); int i = 0; //循环只是为了让程序每两秒钟个进行一次连接侦听是否有消息可以获取 while(true){ rs.consumeMessage(); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } i++; System.out.println("Start recive "+i); } } Session session; public Message consumeMessage() { Message mess = new Message(); List list = new ArrayList(); ConnectionFactory connectionFactory; Connection connection = null; Queue queue; MessageConsumer consumer; connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD,"tcp://127.0.0.1:61616"); try { connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.FALSE, session.AUTO_ACKNOWLEDGE); queue = new ActiveMQQueue("jason.queue2"); consumer = session.createConsumer(queue); while (true) { TextMessage message = (TextMessage) consumer.receive(1000); if (null != message) { System.out.println("收到消息" + message.getText()); list.add(message.getText()); } else { // 没有消息时,退出 break; } } } catch (JMSException e) { e.printStackTrace(); }finally { mess.setPinpai((String)list.get(0)); mess.setShangshi((String)list.get(1)); mess.setWeb((String)list.get(2)); mess.setPrice((String)list.get(3)); try { if (null != connection) connection.close(); } catch (Throwable ignore) { ignore.printStackTrace(); } } return mess; } public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage txtmess = (TextMessage) message; System.out.println("收到的消息是:" + txtmess.getText()); // 回复发送者 MessageProducer sender = session.createProducer(message.getJMSReplyTo()); sender.send(session.createTextMessage("已收到你的消息")); } else System.out.println("收到的消息是:" + message); } catch (Exception e) { e.printStackTrace(); } }}
OK,直接在Java Application上运行一下就好了
如果和Hibernate、Spring集成,首先要将数据库访问工作做好,这里就不再赘述。
然后取得页面控件值,操作如下:
在jsp中,为了可以提交给服务器,需要用Form标签将其包围起来,还要使用id定义控件,如:<input name="txtName" type="text" id="txtName" />
定义了一个ID为 txtName的文本框控件,action="coret.jsp" 则定义了提交后进行处理的JSP页面。
然后,新建core.jsp,如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% MessageService s=new MessageService (); s.add(request); out.write("添加成功!"); %>
这样就将功能代码封装至MessageService.add()方法中了。
然后,就是建立MessageService 类,主要代码如下:
public class MessageService { public static void add(HttpServletRequest request) { String name=request.getParameter("txtName"); }
这样就取到了jsp页面中textName的值了。
以此方法,得到页面的所有值,将其封装成对象,然后调用访问数据库的方法,就OK啦!
- JMS消息服务器和Hibernate、Spring的集成开发
- Spring jms 和 ActiveMQ 开发消息服务
- JSF和Hibernate、Spring的集成开发
- Spring JMS和ActiveMQ集成
- Spring 和 Hibernate的集成
- Spring 和 Hibernate的集成
- spring和hibernate的集成
- SSH开发环境的配置:web.xml、Spring集成hibernate和Spring集成Struts2
- struts ,hibernate和spring集成开发问题
- Spring 里JMS和DB的事务集成
- spring+hibernate集成开发环境的搭建
- spring和hibernate集成
- Spring和Hibernate集成
- spring集成redis和jms时注意
- java消息中间件之二:spring集成JMS连接ActiveMQ
- FLEX和spring、hibernate的集成
- SpringMVC、Spring和Hibernate的集成
- jbpm和spring、hibernate的集成
- Marvell
- java instanceof详解
- jsp url 参数加密传送的终极解决方案
- Ubuntu 10.04下安装配置NFS过程
- 程序出错后,程序员给测试人员的20条高频回复
- JMS消息服务器和Hibernate、Spring的集成开发
- 新开博客
- http://tech.ccidnet.com/art/3737/20080903/1562491_1.html(java环境变量)
- 3GModem开发流程
- oracle 父存储过程调用子过程的事务问题
- java和c#中的String equals == 比较
- Excel中的concatenate函数
- 采用CURL库在PHP程序之间传递数组
- tomcat 配置数据源的两种方式