-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
孙显顺
committed
Jul 23, 2017
1 parent
3026ad0
commit 05152e7
Showing
13 changed files
with
432 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Context path="/"> | ||
<Resource name="jndi/myjdbc" | ||
auth="Container" | ||
type="javax.sql.DataSource" | ||
driverClassName="com.mysql.jdbc.Driver" | ||
url="jdbc:mysql://127.0.0.1:3306/bigdata" | ||
username="root" | ||
password="root" | ||
maxActive="20" | ||
maxIdle="10" | ||
maxWait="10000"/> | ||
|
||
</Context> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
|
||
http://blog.csdn.net/erica_1230/article/details/51661381 | ||
第一种:全局配置。 | ||
1)在tomcat的conf文件夹下的context.xml配置文件中加入: | ||
<Resource name="jndi/mybatis" | ||
auth="Container" | ||
type="javax.sql.DataSource" | ||
driverClassName="com.mysql.jdbc.Driver" | ||
url="jdbc:mysql://localhost:3306/appdb" | ||
username="root" | ||
password="123456" | ||
maxActive="20" | ||
maxIdle="10" | ||
maxWait="10000"/> | ||
2)在项目的web.xml中加入资源引用:其中res-ref-name值要和context.xml的name值一致。 | ||
<resource-ref> | ||
<description>JNDI DataSource</description> | ||
<res-ref-name>jndi/mybatis</res-ref-name> | ||
<res-ref-type>javax.sql.DataSource</res-ref-type> | ||
<res-auth>Container</res-auth> | ||
</resource-ref> | ||
3)jndi测试方法: | ||
public void testJNDI() throws NamingException, SQLException{ | ||
Context ctx = new InitialContext(); | ||
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jndi/mybatis"); | ||
Connection conn = ds.getConnection(); | ||
System.out.println(conn.isClosed()); | ||
|
||
} | ||
4)在jsp中调用加载jndi方式,不可以直接用main方法测试,必须通过启动容器从jsp中调用: | ||
TestPageAccessURL test = new TestPageAccessURL(); | ||
test.testJNDI(); | ||
|
||
第二种:局部配置(不推荐)。 | ||
1)在tomcat的server.xml的<host>标签内,添加: 其他配置同第一种方式。 | ||
<Context path="/demo_jndi" docBase="/demo_jndi"> | ||
<Resource | ||
name="jndi/mybatis" | ||
type="javax.sql.DataSource" | ||
driverClassName="com.mysql.jdbc.Driver" | ||
maxIdle="2" | ||
maxWait="5000" | ||
username="root" | ||
password="123456" | ||
url="jdbc:mysql://localhost:3306/appdb" | ||
maxActive="4"/> | ||
</Context> | ||
|
||
第三种:局部配置。 | ||
1)在项目的META-INFO下面新建context.xml。加入: 其他配置同第一种方式。 | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Context> | ||
<Resource name="jndi/mybatis" | ||
auth="Container" | ||
type="javax.sql.DataSource" | ||
driverClassName="com.mysql.jdbc.Driver" | ||
url="jdbc:mysql://localhost:3306/appdb" | ||
username="root" | ||
password="123456" | ||
maxActive="20" | ||
maxIdle="10" | ||
maxWait="10000"/> | ||
</Context> | ||
|
||
总结:如果要配置局部的话,推荐使用第三种方式,这样不依赖tomcat了。但是还是推荐使用第一种方式好,虽然依赖tomat,但是是全局的,而且可以配置 | ||
多个。对于以后切换使用方便。 | ||
在项目的web.xml中添加的资源引用可有可无。 | ||
|
||
|
||
|
||
https://wenku.baidu.com/view/be1901bf1a37f111f1855b7e.html | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.sxshunrj.test; | ||
|
||
import javax.naming.InitialContext; | ||
import javax.naming.NamingException; | ||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: sunxs | ||
* Date: 2017/7/23 15:55 | ||
* Desc: | ||
*/ | ||
public class MTest { | ||
public static void main(String[] args) throws NamingException, SQLException { | ||
Properties props = new Properties(); | ||
props.setProperty("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory"); | ||
|
||
InitialContext ctx = new InitialContext(props); | ||
// DataSource ds = (DataSource) ctx.lookup("java:comp/env/jndi/mybatis");//java:comp/env/是一个J2EE环境的定义 | ||
DataSource ds = (DataSource) ctx.lookup("jndi/myjdbc");//java:comp/env/是一个J2EE环境的定义 | ||
Connection conn = ds.getConnection(); | ||
ResultSet resultSet = conn.createStatement().executeQuery("select * from tb_area"); | ||
while (resultSet.next()){ | ||
System.out.println(resultSet.getString("description")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.sxshunrj.test; | ||
|
||
import javax.naming.InitialContext; | ||
import javax.jms.Topic; | ||
import javax.jms.Session; | ||
import javax.jms.TextMessage; | ||
import javax.jms.TopicPublisher; | ||
import javax.jms.DeliveryMode; | ||
import javax.jms.TopicSession; | ||
import javax.jms.TopicConnection; | ||
import javax.jms.TopicConnectionFactory; | ||
|
||
public class Publisher | ||
{ | ||
public static void main(String[] args) throws Exception | ||
{ | ||
// get the initial context | ||
InitialContext ctx = new InitialContext(); | ||
|
||
// lookup the topic object | ||
Topic topic = (Topic) ctx.lookup("topic/topic0"); | ||
|
||
// lookup the topic connection factory | ||
TopicConnectionFactory connFactory = (TopicConnectionFactory) ctx. | ||
lookup("topic/connectionFactory"); | ||
|
||
// create a topic connection | ||
TopicConnection topicConn = connFactory.createTopicConnection(); | ||
|
||
// create a topic session | ||
TopicSession topicSession = topicConn.createTopicSession(false, | ||
Session.AUTO_ACKNOWLEDGE); | ||
|
||
// create a topic publisher | ||
TopicPublisher topicPublisher = topicSession.createPublisher(topic); | ||
topicPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT); | ||
|
||
// create the "Hello World" message | ||
TextMessage message = topicSession.createTextMessage(); | ||
message.setText("Hello World"); | ||
|
||
// publish the messages | ||
topicPublisher.publish(message); | ||
|
||
// print what we did | ||
System.out.println("Message published: " + message.getText()); | ||
|
||
// close the topic connection | ||
topicConn.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.sxshunrj.test; | ||
|
||
import javax.jms.*; | ||
import javax.naming.InitialContext; | ||
|
||
public class QueueMessageClient { | ||
public static void main(String[] args)throws Exception { | ||
InitialContext context = new InitialContext(); | ||
//获取QueueConnectionFactory对象 | ||
QueueConnectionFactory factory=(QueueConnectionFactory)context.lookup("ConnectionFactory"); | ||
//创建QueueConnection | ||
QueueConnection connection=factory.createQueueConnection(); | ||
//创建QueueSession对象 | ||
QueueSession session=connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); | ||
//获取Destination对象 | ||
Queue queue =(Queue)context.lookup("queue/myqueue"); | ||
//创建文本消息 | ||
TextMessage msg=session.createTextMessage("Hello World --世界,你好"); | ||
//创建发送者 | ||
QueueSender sender=session.createSender(queue); | ||
//发送消息 | ||
sender.send(msg); | ||
//关闭会话 | ||
session.close(); | ||
connection.close(); | ||
|
||
System.out.println("消息已发送"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.sxshunrj.test; | ||
|
||
import javax.naming.InitialContext; | ||
import javax.jms.Queue; | ||
import javax.jms.Session; | ||
import javax.jms.TextMessage; | ||
import javax.jms.QueueSession; | ||
import javax.jms.QueueReceiver; | ||
import javax.jms.QueueConnection; | ||
import javax.jms.QueueConnectionFactory; | ||
|
||
public class Receiver | ||
{ | ||
public static void main(String[] args) throws Exception | ||
{ | ||
// get the initial context | ||
InitialContext context = new InitialContext(); | ||
|
||
// lookup the queue object | ||
Queue queue = (Queue) context.lookup("queue/queue0"); | ||
|
||
// lookup the queue connection factory | ||
QueueConnectionFactory conFactory = (QueueConnectionFactory) context.lookup ("queue/connectionFactory"); | ||
|
||
// create a queue connection | ||
QueueConnection queConn = conFactory.createQueueConnection(); | ||
|
||
// create a queue session | ||
QueueSession queSession = queConn.createQueueSession(false, | ||
Session.AUTO_ACKNOWLEDGE); | ||
|
||
// create a queue receiver | ||
QueueReceiver queueReceiver = queSession.createReceiver(queue); | ||
|
||
// start the connection | ||
queConn.start(); | ||
|
||
// receive a message | ||
TextMessage message = (TextMessage) queueReceiver.receive(); | ||
|
||
// print the message | ||
System.out.println("Message Received: " + message.getText()); | ||
|
||
// close the queue connection | ||
queConn.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.sxshunrj.test; | ||
|
||
import javax.naming.InitialContext; | ||
|
||
import javax.jms.Queue; | ||
import javax.jms.Session; | ||
import javax.jms.TextMessage; | ||
import javax.jms.QueueSender; | ||
import javax.jms.DeliveryMode; | ||
import javax.jms.QueueSession; | ||
import javax.jms.QueueConnection; | ||
import javax.jms.QueueConnectionFactory; | ||
|
||
public class Sender | ||
{ | ||
public static void main(String[] args) throws Exception | ||
{ | ||
// get the initial context | ||
InitialContext context = new InitialContext(); | ||
|
||
// lookup the queue object | ||
Queue queue = (Queue) context.lookup("queue/queue0"); | ||
|
||
// lookup the queue connection factory | ||
QueueConnectionFactory conFactory = (QueueConnectionFactory) context.lookup ("queue/connectionFactory"); | ||
|
||
// create a queue connection | ||
QueueConnection queConn = conFactory.createQueueConnection(); | ||
|
||
// create a queue session | ||
QueueSession queSession = queConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE); | ||
|
||
// create a queue sender | ||
QueueSender queueSender = queSession.createSender(queue); | ||
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT); | ||
|
||
// create a simple message to say "Hello World" | ||
TextMessage message = queSession.createTextMessage("Hello World"); | ||
|
||
// send the message | ||
queueSender.send(message); | ||
|
||
// print what we did | ||
System.out.println("Message Sent: " + message.getText()); | ||
|
||
// close the queue connection | ||
queConn.close(); | ||
} | ||
} |
Oops, something went wrong.