📄 mdbsteps.txt
字号:
package jsmtest;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.jms.QueueConnectionFactory;
import javax.jms.Queue;
import javax.jms.JMSException;
import javax.jms.QueueSender;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
public class MessageProducer {
public MessageProducer() {
}
public static void main( String args[] ){
Properties jndiEnv = new Properties();
jndiEnv.setProperty(Context.INITIAL_CONTEXT_FACTORY ,
"weblogic.jndi.WLInitialContextFactory" );
jndiEnv.setProperty(Context.PROVIDER_URL , "t3://localhost:7001" );
QueueConnection conn = null;
QueueSession session = null;
QueueSender sender = null;
Queue q = null;
try{
Context ctx = new InitialContext( jndiEnv );
QueueConnectionFactory factory = ( QueueConnectionFactory )ctx.lookup(
"jms/QueueConnectionFactory" );
conn = factory.createQueueConnection();
session = conn.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
q = (Queue) ctx.lookup( "jms/FileQueue" );
sender = session.createSender(q);
TextMessage msg = session.createTextMessage();
msg.setText( "hello world" );
sender.send( msg );
System.out.println( "=== send message ok ===" );
}catch( NamingException ne ){
ne.printStackTrace();
}
catch( JMSException je ){
je.printStackTrace();
}
finally{
if( sender != null ) try{ sender.close();} catch( Exception e ){}
if( session != null ) try{ session.close();} catch( Exception e ){}
if( conn != null ) try{ conn.close();} catch( Exception e ){}
}
}
}
=========================================================================================
package jsmtest;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.jms.Queue;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import java.util.Properties;
public class MessageConsumer {
public MessageConsumer() {
}
public static void main( String args[] ){
Properties jndiEnv = new Properties();
jndiEnv.setProperty( Context.INITIAL_CONTEXT_FACTORY ,
"weblogic.jndi.WLInitialContextFactory");
jndiEnv.setProperty( Context.PROVIDER_URL , "t3://localhost:7001" );
QueueConnection conn = null;
QueueSession session = null;
QueueReceiver receiver = null;
try{
Context ctx = new InitialContext( jndiEnv );
QueueConnectionFactory factory = ( QueueConnectionFactory )
ctx.lookup( "jms/QueueConnectionFactory" );
conn = factory.createQueueConnection();
conn.start();
session = conn.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
Queue q = (Queue)ctx.lookup( "jms/fileQueue" );
receiver = session.createReceiver( q );
TextMessage msg = (TextMessage)receiver.receive();
System.out.println( "get message ===> " + msg.getText() );
}catch( NamingException ne ){
ne.printStackTrace();
}
catch( JMSException je ){
je.printStackTrace();
}
finally{
if( receiver != null )try{ receiver.close(); } catch(Exception e ){}
if( session != null )try{ session.close(); } catch(Exception e ){}
if( conn != null )try{ conn.close(); } catch(Exception e ){}
}
}
}
======================================================================================
在JBUILDER中创建MDB(HelloMDB):
1. File->new->Enterprise->EJB Module
Name: mdbs
->OK
2. 在EJB Designer中按右键
Create EJB->Message-Driven Bean
3.修改MDB属性:
Message-Driven Bean properties(EJB规范):
Bean name: MessageEJB
Transaction type: Bean
Acknowledge mode: Auto-acknowledge
Destination type: javax.jms.Queue
Binding properties(与weblogic相关的属性):
Destination name: jms/fileQueue
Connection factory name: jms/QueueConnectionFactory
4. 选择HelloMDB, ->View Bean Source
修改方法onMessage:
public void onMessage(Message msg) {
TextMessage tMsg = (TextMessge)msg;
try{
System.out.println( "get message =======> " + tMsg.getText() );
}catch( JMSException je ){
je.printStackTrace();
}
}
5. make and deploy mdbs
6, test mdb use MessageProducer class
================================================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -