Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
9bb6e0f4
Commit
9bb6e0f4
authored
Sep 26, 2013
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix test
parent
941d1637
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
148 additions
and
136 deletions
+148
-136
RabbitTemplateAutoConfiguration.java
...t/autoconfigure/amqp/RabbitTemplateAutoConfiguration.java
+76
-74
RabbitTemplateAutoconfigurationTests.java
...oconfigure/amqp/RabbitTemplateAutoconfigurationTests.java
+72
-62
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateAutoConfiguration.java
View file @
9bb6e0f4
...
...
@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.amqp;
import
org.springframework.amqp.core.AmqpAdmin
;
import
org.springframework.amqp.rabbit.connection.CachingConnectionFactory
;
import
org.springframework.amqp.rabbit.connection.ConnectionFactory
;
import
org.springframework.amqp.rabbit.core.RabbitAdmin
;
import
org.springframework.amqp.rabbit.core.RabbitTemplate
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -32,7 +33,7 @@ import org.springframework.context.annotation.Configuration;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link RabbitTemplate}.
*
*
* @author Greg Turnquist
*/
@Configuration
...
...
@@ -40,101 +41,102 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties
public
class
RabbitTemplateAutoConfiguration
{
@Bean
@ConditionalOnExpression
(
"${spring.rabbitmq.dynamic:true}"
)
@ConditionalOnMissingBean
(
AmqpAdmin
.
class
)
public
AmqpAdmin
amqpAdmin
(
CachingConnectionFactory
connectionFactory
)
{
return
new
RabbitAdmin
(
connectionFactory
);
}
@Bean
@ConditionalOnExpression
(
"${spring.rabbitmq.dynamic:true}"
)
@ConditionalOnMissingBean
(
AmqpAdmin
.
class
)
public
AmqpAdmin
amqpAdmin
(
CachingConnectionFactory
connectionFactory
)
{
return
new
RabbitAdmin
(
connectionFactory
);
}
@Configuration
@ConditionalOnMissingBean
(
RabbitTemplate
.
class
)
protected
static
class
RabbitTemplateCreator
{
@Configuration
@ConditionalOnMissingBean
(
RabbitTemplate
.
class
)
protected
static
class
RabbitTemplateCreator
{
@Autowired
CachingConnectionFactory
connectionFactory
;
@Autowired
CachingConnectionFactory
connectionFactory
;
@Bean
public
RabbitTemplate
rabbitTemplate
()
{
return
new
RabbitTemplate
(
this
.
connectionFactory
);
}
@Bean
public
RabbitTemplate
rabbitTemplate
()
{
return
new
RabbitTemplate
(
this
.
connectionFactory
);
}
}
}
@Configuration
@ConditionalOnMissingBean
(
Caching
ConnectionFactory
.
class
)
@EnableConfigurationProperties
(
RabbitConnectionFactoryProperties
.
class
)
protected
static
class
RabbitConnectionFactoryCreator
{
@Configuration
@ConditionalOnMissingBean
(
ConnectionFactory
.
class
)
@EnableConfigurationProperties
(
RabbitConnectionFactoryProperties
.
class
)
protected
static
class
RabbitConnectionFactoryCreator
{
@Autowired
private
RabbitConnectionFactoryProperties
config
;
@Autowired
private
RabbitConnectionFactoryProperties
config
;
@Bean
public
CachingConnectionFactory
connectionFactory
()
{
CachingConnectionFactory
connectionFactory
=
new
CachingConnectionFactory
(
this
.
config
.
getHost
());
connectionFactory
.
setPort
(
this
.
config
.
getPort
());
if
(
this
.
config
.
getUsername
()
!=
null
)
{
connectionFactory
.
setUsername
(
this
.
config
.
getUsername
());
}
if
(
this
.
config
.
getPassword
()
!=
null
)
{
connectionFactory
.
setPassword
(
this
.
config
.
getPassword
());
}
return
connectionFactory
;
}
}
@Bean
public
CachingConnectionFactory
connectionFactory
()
{
CachingConnectionFactory
connectionFactory
=
new
CachingConnectionFactory
(
this
.
config
.
getHost
());
connectionFactory
.
setPort
(
this
.
config
.
getPort
());
if
(
this
.
config
.
getUsername
()
!=
null
)
{
connectionFactory
.
setUsername
(
this
.
config
.
getUsername
());
}
if
(
this
.
config
.
getPassword
()
!=
null
)
{
connectionFactory
.
setPassword
(
this
.
config
.
getPassword
());
}
return
connectionFactory
;
}
}
@ConfigurationProperties
(
name
=
"spring.rabbitmq"
)
public
static
class
RabbitConnectionFactoryProperties
{
@ConfigurationProperties
(
name
=
"spring.rabbitmq"
)
public
static
class
RabbitConnectionFactoryProperties
{
private
String
host
=
"localhost"
;
private
String
host
=
"localhost"
;
private
int
port
=
5672
;
private
int
port
=
5672
;
private
String
username
;
private
String
username
;
private
String
password
;
private
String
password
;
private
boolean
dynamic
=
true
;
private
boolean
dynamic
=
true
;
public
String
getHost
()
{
return
host
;
}
public
String
getHost
()
{
return
this
.
host
;
}
public
void
setHost
(
String
host
)
{
this
.
host
=
host
;
}
public
void
setHost
(
String
host
)
{
this
.
host
=
host
;
}
public
int
getPort
()
{
return
port
;
}
public
int
getPort
()
{
return
this
.
port
;
}
public
void
setPort
(
int
port
)
{
this
.
port
=
port
;
}
public
void
setPort
(
int
port
)
{
this
.
port
=
port
;
}
public
String
getUsername
()
{
return
username
;
}
public
String
getUsername
()
{
return
this
.
username
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
username
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
username
;
}
public
String
getPassword
()
{
return
password
;
}
public
String
getPassword
()
{
return
this
.
password
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
public
boolean
isDynamic
()
{
return
dynamic
;
}
public
boolean
isDynamic
()
{
return
this
.
dynamic
;
}
public
void
setDynamic
(
boolean
dynamic
)
{
this
.
dynamic
=
dynamic
;
}
public
void
setDynamic
(
boolean
dynamic
)
{
this
.
dynamic
=
dynamic
;
}
}
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateAutoconfigurationTests.java
View file @
9bb6e0f4
...
...
@@ -27,80 +27,90 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
static
junit
.
framework
.
Assert
.
assertEquals
;
import
static
junit
.
framework
.
Assert
.
assertNotNull
;
import
static
junit
.
framework
.
Assert
.
fail
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
fail
;
/**
* Tests for {@link RabbitTemplateAutoConfiguration}.
*
*
* @author Greg Turnquist
*/
public
class
RabbitTemplateAutoconfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
private
AnnotationConfigApplicationContext
context
;
@Test
public
void
testDefaultRabbitTemplate
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
TestConfiguration
.
class
,
RabbitTemplateAutoConfiguration
.
class
);
this
.
context
.
refresh
();
RabbitTemplate
rabbitTemplate
=
this
.
context
.
getBean
(
RabbitTemplate
.
class
);
CachingConnectionFactory
connectionFactory
=
this
.
context
.
getBean
(
CachingConnectionFactory
.
class
);
RabbitAdmin
amqpAdmin
=
this
.
context
.
getBean
(
RabbitAdmin
.
class
);
assertNotNull
(
rabbitTemplate
);
assertNotNull
(
connectionFactory
);
assertNotNull
(
amqpAdmin
);
assertEquals
(
rabbitTemplate
.
getConnectionFactory
(),
connectionFactory
);
assertEquals
(
connectionFactory
.
getHost
(),
"localhost"
);
}
@Test
public
void
testDefaultRabbitTemplate
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
TestConfiguration
.
class
,
RabbitTemplateAutoConfiguration
.
class
);
this
.
context
.
refresh
();
RabbitTemplate
rabbitTemplate
=
this
.
context
.
getBean
(
RabbitTemplate
.
class
);
CachingConnectionFactory
connectionFactory
=
this
.
context
.
getBean
(
CachingConnectionFactory
.
class
);
RabbitAdmin
amqpAdmin
=
this
.
context
.
getBean
(
RabbitAdmin
.
class
);
assertNotNull
(
rabbitTemplate
);
assertNotNull
(
connectionFactory
);
assertNotNull
(
amqpAdmin
);
assertEquals
(
rabbitTemplate
.
getConnectionFactory
(),
connectionFactory
);
assertEquals
(
connectionFactory
.
getHost
(),
"localhost"
);
}
@Test
public
void
testRabbitTemplateWithOverrides
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
TestConfiguration
.
class
,
RabbitTemplateAutoConfiguration
.
class
);
TestUtils
.
addEnviroment
(
this
.
context
,
"spring.rabbitmq.host:remote-server"
,
"spring.rabbitmq.port:9000"
,
"spring.rabbitmq.username:alice"
,
"spring.rabbitmq.password:secret"
);
this
.
context
.
refresh
();
CachingConnectionFactory
connectionFactory
=
this
.
context
.
getBean
(
CachingConnectionFactory
.
class
);
assertEquals
(
connectionFactory
.
getHost
(),
"remote-server"
);
assertEquals
(
connectionFactory
.
getPort
(),
9000
);
}
@Test
public
void
testRabbitTemplateWithOverrides
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
TestConfiguration
.
class
,
RabbitTemplateAutoConfiguration
.
class
);
TestUtils
.
addEnviroment
(
this
.
context
,
"spring.rabbitmq.host:remote-server"
,
"spring.rabbitmq.port:9000"
,
"spring.rabbitmq.username:alice"
,
"spring.rabbitmq.password:secret"
);
this
.
context
.
refresh
();
CachingConnectionFactory
connectionFactory
=
this
.
context
.
getBean
(
CachingConnectionFactory
.
class
);
assertEquals
(
connectionFactory
.
getHost
(),
"remote-server"
);
assertEquals
(
connectionFactory
.
getPort
(),
9000
);
}
@Test
public
void
testConnectionFactoryBackoff
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
TestConfiguration2
.
class
,
RabbitTemplateAutoConfiguration
.
class
);
this
.
context
.
refresh
();
RabbitTemplate
rabbitTemplate
=
this
.
context
.
getBean
(
RabbitTemplate
.
class
);
CachingConnectionFactory
connectionFactory
=
this
.
context
.
getBean
(
CachingConnectionFactory
.
class
);
assertEquals
(
rabbitTemplate
.
getConnectionFactory
(),
connectionFactory
);
assertEquals
(
connectionFactory
.
getHost
(),
"otherserver"
);
assertEquals
(
connectionFactory
.
getPort
(),
8001
);
}
@Test
public
void
testConnectionFactoryBackoff
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
TestConfiguration2
.
class
,
RabbitTemplateAutoConfiguration
.
class
);
this
.
context
.
refresh
();
RabbitTemplate
rabbitTemplate
=
this
.
context
.
getBean
(
RabbitTemplate
.
class
);
CachingConnectionFactory
connectionFactory
=
this
.
context
.
getBean
(
CachingConnectionFactory
.
class
);
assertEquals
(
rabbitTemplate
.
getConnectionFactory
(),
connectionFactory
);
assertEquals
(
connectionFactory
.
getHost
(),
"otherserver"
);
assertEquals
(
connectionFactory
.
getPort
(),
8001
);
}
@Test
public
void
testStaticQueues
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
TestConfiguration
.
class
,
RabbitTemplateAutoConfiguration
.
class
);
TestUtils
.
addEnviroment
(
this
.
context
,
"spring.rabbitmq.dynamic:false"
);
this
.
context
.
refresh
();
try
{
this
.
context
.
getBean
(
AmqpAdmin
.
class
);
fail
(
"There should NOT be an AmqpAdmin bean when dynamic is switch to false"
);
}
catch
(
Exception
e
)
{}
}
@Test
public
void
testStaticQueues
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
TestConfiguration
.
class
,
RabbitTemplateAutoConfiguration
.
class
);
TestUtils
.
addEnviroment
(
this
.
context
,
"spring.rabbitmq.dynamic:false"
);
this
.
context
.
refresh
();
try
{
this
.
context
.
getBean
(
AmqpAdmin
.
class
);
fail
(
"There should NOT be an AmqpAdmin bean when dynamic is switch to false"
);
}
catch
(
Exception
e
)
{
}
}
@Configuration
protected
static
class
TestConfiguration
{
@Configuration
protected
static
class
TestConfiguration
{
}
}
@Configuration
protected
static
class
TestConfiguration2
{
@Bean
ConnectionFactory
aDifferentConnectionFactory
()
{
return
new
CachingConnectionFactory
(
"otherserver"
,
8001
);
}
}
@Configuration
protected
static
class
TestConfiguration2
{
@Bean
ConnectionFactory
aDifferentConnectionFactory
()
{
return
new
CachingConnectionFactory
(
"otherserver"
,
8001
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment