JMS Unable to resolve QueueConnectionFactory - queue

volks!
I have a problem. I want to read some messages from Weblogic 12c. This code is currently working with another server, which wasn't configured by me. So the problem may be in configuration of server.
The problem is when I use code:
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY="QCF";
public final static String QUEUE="dizzyworldQueue";
public final static String USER_NAME="weblogic";
public final static String USER_PASSWORD="Welcome1";
public final static String URL="t3://localhost:8001";
private static InitialContext getInitialContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_PRINCIPAL, USER_NAME);
env.put(Context.SECURITY_CREDENTIALS, USER_PASSWORD);
return new InitialContext(env);
}
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection(); // here it falls
The error is:
Exception in thread "main" javax.naming.NameNotFoundException: Unable to resolve 'QCF'. Resolved '' [Root exception is javax.naming.NameNotFoundException: Unable to resolve 'QCF'. Resolved '']; remaining name 'QCF'
I have Queue and QueueConnection factory on my server. What can be the rison?

The message says that there is no connection factory bound in your naming service (JNDI) with the name "QCF".
So you have to configure your application server to offer the queue connection factory through JNDI.
If that is already the case, you have to change that name in your client code to match the name in JNDI.
You have to add the connection factory and the queue in the admin console.
Look for Services - JMS modules. Possibly add a JMS module, and add a connection factory and queue.
Have a look at this tutorial, it shows the steps to configure it.

Related

how to give mongodb socketkeepalive in spring boot application?

In spring boot if we want to connect to mongodb, we can create a configuration file for mongodb or writing datasource in application.properties
I am following the second way
For me, I am gettint this error
"Timeout while receiving message; nested exception is com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
.
spring.data.mongodb.uri = mongodb://mongodb0.example.com:27017/admin
I am gettint this error If I am not using my app for 6/7 hours and after that If I try to hit any controller to retrieve data from Mongodb. After 1/2 try I am able to get
Question - Is it the normal behavior of mongodb?
So, in my case it is closing the socket after some particular hours
I read some blogs where it was written you can give socket-keep-alive, so the connection pool will not close
In spring boot mongodb connection, we can pass options in uri like
spring.data.mongodb.uri = mongodb://mongodb0.example.com:27017/admin/?replicaSet=test&connectTimeoutMS=300000
So, I want to give socket-keep-alive options for spring.data.mongodb.uri like replicaset here.
I searched the official site, but can't able to find any
You can achieve this by providing a MongoClientOptions bean. Spring Data's MongoAutoConfiguration will pick this MongoClientOptions bean up and use it further on:
#Bean
public MongoClientOptions mongoClientOptions() {
return MongoClientOptions.builder()
.socketKeepAlive(true)
.build();
}
Also note that the socket-keep-alive option is deprecated (and defaulted to true) since mongo-driver version 3.5 (used by spring-data since version 2.0.0 of spring-data-mongodb)
You can achieve to pass this option using MongoClientOptionsFactoryBean.
public MongoClientOptions mongoClientOptions() {
try {
final MongoClientOptionsFactoryBean bean = new MongoClientOptionsFactoryBean();
bean.setSocketKeepAlive(true);
bean.afterPropertiesSet();
return bean.getObject();
} catch (final Exception e) {
throw new BeanCreationException(e.getMessage(), e);
}
}
Here an example of this configuration by extending AbstractMongoConfiguration:
#Configuration
public class DataportalApplicationConfig extends AbstractMongoConfiguration {
//#Value: inject property values into components
#Value("${spring.data.mongodb.uri}")
private String uri;
#Value("${spring.data.mongodb.database}")
private String database;
/**
* Configure the MongoClient with the uri
*
* #return MongoClient.class
*/
#Override
public MongoClient mongoClient() {
return new MongoClient(new MongoClientURI(uri,mongoClientOptions().builder()));
}

How to Disable Ribbon and just use FeignClient in Spring Cloud

I am aware that we can force FeignClient to use OkHttp instead of Ribbon by providing the url Ex. #FeignClient(url="serviceId", name="serviceId")
I want the OkHttpClient to be used even when just the name is provided. Ex. #FeignClient(name="serviceId")
As per the spring cloud documentation "if Ribbon is enabled it is a LoadBalancerFeignClient, otherwise the default feign client is used."
How can I disable ribbon so that the default feign client will be used.
None of the solutions on the internet worked for me.
Simply setting an absolute url in the url portion resulted in loadbalancing exceptions
// this resulted in java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: localhost
#Lazy
#Configuration
#Import(FeignClientsConfiguration.class)
public class MyConfig {
#LocalServerPort
private int port;
#Bean
public MyClient myClient(final Decoder decoder, final Encoder encoder, final Client client) {
return Feign.builder().client(client)
.encoder(encoder)
.decoder(decoder)
.target(MyClient.class, "http://localhost:" + localServerPort);
}
}
setting spring.cloud.loadbalancing.ribbon.enabled=false resulted in application context problems. Additional settings needs to be disabled for this to work. I did not probe further
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eurekaLoadBalancerClientConfiguration': Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
...
...
My working solution
Finally, after inspecting the source code in org.springframework.cloud.openfeign.ribbon.DefaultFeignLoadBalancedConfiguration, I came up with this solution
#Lazy // required for #LocalServerPort to work in a #Configuration/#TestConfiguration
#TestConfiguration
#Import(FeignClientsConfiguration.class)
public class MyConfig {
#LocalServerPort
private int port;
#Bean
public MyClient myClient(Decoder decoder, Encoder encoder, Client client, Contract contract) {
return Feign.builder().client(client)
.encoder(encoder)
.decoder(decoder)
.contract(contract)
.target(MyClient.class, "http://localhost:" + localServerPort);
}
// provide a default `FeignClient` so that Spring will not automatically create their LoadBalancingFeignClient
#Bean
public Client feignClient(SpringClientFactory clientFactory) {
return new Client.Default(null, null);
}
}
I had the same question but my setup is a bit different and I did not get it working in my case (using spring-cloud-starter-openfeign with spring mvc style annotations).
FYI: I needed a custom client with an SSLSocketFactory and ended up just creating the bean for the client and keeping the url on #FeignClient
#Bean
public Client myClient() {
return new Client.Default(getSSLSocketFactory(), new NoopHostnameVerifier());
}
However, we do have projects using spring-cloud-starter-feign where the URL is not provided on the annotation. Not sure if the config below is complete (I did not set it up) but it might point you in the right direction...
dependencies
compile("org.springframework.cloud:spring-cloud-starter-feign") {
exclude group: 'org.springframework.cloud', module: 'spring-cloud-starter-ribbon'
exclude group: 'org.springframework.cloud', module: 'spring-cloud-starter-archaius'
}
config
#Configuration
#Import(FeignClientsConfiguration.class) // org.springframework.cloud.netflix.feign.FeignClientsConfiguration
public class MyConfig {
#Value("${client.url}")
private String url;
#Bean
public MyClient myClient(final Decoder decoder, final Encoder encoder, final Client client) {
return Feign.builder().client(client)
.encoder(encoder)
.decoder(decoder)
.target(MyClient.class, url);
}
}
It has nothing to do with Ribbon.
Check this:
feign:
httpclient:
enabled: false
This will disable the spring cloud autoconfigured httpclient, and will search a #Bean named httpClient in the context. So provide the definition of #Bean in a #Configuration class and that's all.
Check class FeignAutoConfiguration in spring cloud feign.
https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html

Exception in thread "main" com.twilio.exception.ApiException: api.twilio.com

public static final String ACCOUNT_SID = "ACxxx";
public static final String AUTH_TOKEN = "6xx";
public static void main(String[] args) {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Message message =
Message.creator(new PhoneNumber("+1518xx"), new PhoneNumber("+18xxx"),
"SMS POC").create();
System.out.println(message.getSid());
}
Exception
Exception in thread "main" com.twilio.exception.ApiException: api.twilio.com
at com.twilio.http.NetworkHttpClient.makeRequest(NetworkHttpClient.java:88)
at com.twilio.http.HttpClient.reliableRequest(HttpClient.java:38)
at com.twilio.http.HttpClient.reliableRequest(HttpClient.java:22)
at com.twilio.http.TwilioRestClient.request(TwilioRestClient.java:42)
at com.twilio.rest.api.v2010.account.MessageCreator.create(MessageCreator.java:307)
at com.twilio.rest.api.v2010.account.MessageCreator.create(MessageCreator.java:25)
at com.twilio.base.Creator.create(Creator.java:45)
at com.twilio.notifications.config.TestMain.main(TestMain.java:18)
I ran into this issue as well when trying out the quick start example. I verified that I had the correct verified phone numbers, the text did not have an empty message and the code specified the to and from numbers properly.
I found the issue to be proxy related. Once fixed this in my local setup, it worked like a charm. It also worked without any issues in the server.
Hope this helps,
Alice

Test the remote client jndi lookup using arquillian

Setup: arquillian, jboss as 7.1.1.final as a managed Container
I am currently migrating an EJB application from EJB 2.x to 3.x and JBoss 3.x to JBoss AS 7.1.
During this process i would like to get most classes under test and stumbled over arquillian.
While arquillian seems to offer some nice features on inter-bean-functionality i cannot figure out whether or not the testing of remote client features using jndi lookups works or not.
I used the Arquillian Getting started guides on my beans which worked, but since these are using #Inject and in my application jndi lookups are used everywhere i (at least think that i) need to swerve from that path.
Here is the TestCase i created based on Arquillian Getting Started. I explicitly left in all attempts using jndi properties of which i thought they might help.
The Test
should_create_greeting()
works if the Greeter bean using a separate Producer.
#RunWith(Arquillian.class)
public class GreeterTest {
public static final String ARCHIVE_NAME = "test";
Logger logger = Logger.getLogger(GreeterTest.class.getName());
#Deployment
public static Archive<?> createDeployment() {
JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar").addPackage(Greeter.class.getPackage())
.addAsManifestResource("test-persistence.xml", "persistence.xml").addAsManifestResource("OracleGUIDS-ds.xml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
return jar;
}
/**
* #Inject works using a producer with {#code #Produces}
*/
// #Inject
// Greeter greeter;
#ArquillianResource
Context context;
GreeterRemote greeter;
#Before
public void before() throws Exception {
Map<String, String> env = new HashMap<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
env.put("jboss.naming.client.ejb.context", "true");
// env.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT",
// "false");
// env.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS",
// "false");
// env.put("jboss.naming.client.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED",
// "false");
for (Map.Entry<String, String> entry : env.entrySet()) {
context.addToEnvironment(entry.getKey(), entry.getValue());
}
greeter = (GreeterRemote) context.lookup(ARCHIVE_NAME + "/" + Greeter.class.getSimpleName() + "!"
+ GreeterRemote.class.getName());
}
#Test
public void should_create_greeting() {
Assert.assertEquals("Hello, Earthling!", greeter.createGreeting("Earthling"));
greeter.greet(System.out, "Earthling");
}
}
Is it possible to get this test running with jndi lookup? Am i missing something?
If you want to test the Remote features of a EJB you probably want to run on the client side and not in container.
You can configure the Deployment to be only client side by using #Deployment(testable=false). The #Test methods will then run as if you were a remote client.
Beyond that you can just lookup the bean via the injected Context if you want.
I had the same issue, so in a workaround i just added on the method to be tested the remoteejb as a parameter.
On my ejb:
public List localBean.obtain(RemoteEJB remoteEjb){
return remoteEjb.obtain();
}
Then on the arquillian test :
#Inject
private LocalBean localBean;
#Inject
private RemoteEJB remoteEjb;
#Test
public void test(){
List<Vo>voList = localBean.obtain(remoteEjb);
}
The best part is the remote ejb its injected and on the caller method original
#EJB(lookup="java:global/ear/ejb/RemoteEjb")
private RemoteEJB remoteEjb;

additional MBeanServer and JMXConnectorServer for JBoss 7

There are security reasons that I cannot add my MBeans to the existing JBoss 7 platform MBeanServer. So I create my own mBeanServer and JMXConnectorServer with a customAuthenticator.
Here are my Spring Bean definition for new MBeanServer and JMXConnectorServer. This code works when I run my application in Jetty. I was able to connect via URL service:jmx:rmi://localhost/jndi/rmi://localhost:17999/sample in jconsole and it only shows the custom MBeans which is what I expect.
But the same code does not work in JBoss 7. When I deploy to JBoss and try to connect with the same JMX URL, it gives a dialog with this error: "The connection to myuser#service:jmx:rmi://localhost/jndi/rmi://localhost:17999/trm did not succeed. Would you like to try again?"
I put a break point in my customAuthenticator and JBoss doesn't stop at my break point when I attempt to connect JMX. It seems my JMXConnectorServer is not being used by JBoss. Can anyone help? Note that I cannot change the existing JBoss MBeanServer or JMX Connector Server configuration because they are used for other purpose.
Thanks in advance.
#Bean
public Object rmiRegistry() throws Exception {
RmiRegistryFactoryBean factory = new RmiRegistryFactoryBean();
factory.setPort(17999);
factory.afterPropertiesSet();
return factory.getObject();
}
#Bean
#DependsOn("rmiRegistry")
public MBeanServer mBeanServer() {
MBeanServerFactoryBean factory = new MBeanServerFactoryBean();
factory.afterPropertiesSet();
return factory.getObject();
}
#Bean
#DependsOn("rmiRegistry")
public JMXConnectorServer jmxConnectorServer() throws IOException, JMException {
ConnectorServerFactoryBean factory = new ConnectorServerFactoryBean();
factory.setServer(mBeanServer());
factory.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:17999/sample");
factory.setRegistrationPolicy(RegistrationPolicy.FAIL_ON_EXISTING);
Map<String, Object> props = new HashMap<>();
props.put(JMXConnectorServer.AUTHENTICATOR, customAuthenticator);
factory.setEnvironmentMap(props);
factory.afterPropertiesSet();
return factory.getObject();
}
#Bean
#DependsOn("rmiRegistry")
public AnnotationMBeanExporter annotationMBeanExporter() {
AnnotationMBeanExporter result = null;
result = new AnnotationMBeanExporter();
result.setServer(mBeanServer());
return result;
}
I suspect the JBoss environment is influencing how the JMX Connector server is configured. I would try taking the extra step of specifying the service listening port (e.g. 17998) rather than leaving it as an ephemeral by using this JMXServiceURL:
service:jmx:rmi://localhost:17998/jndi/rmi://localhost:17999/sample